Error response format
Errors return a JSON body with anerror object and an appropriate HTTP status:
type field is the machine-readable discriminator. Common values:
type | When |
|---|---|
authentication_error | Missing or invalid API key (401). |
invalid_request_error | Malformed body or an unsupported parameter (400). |
model_not_found | The model id is not available (400). |
upstream_error | The upstream provider is unavailable (502). |
Status codes
| Status | Meaning |
|---|---|
| 400 | Bad request (invalid parameters or unknown model) |
| 401 | Unauthorized (missing or invalid API key) |
| 403 | Forbidden (for example, insufficient credits) |
| 429 | Rate limit exceeded |
| 500 | Server error |
| 502 | Upstream provider unavailable |
| 503 | Service temporarily unavailable |
Handle errors with the SDK
OpenAI client libraries map these statuses to typed exceptions:Python
JavaScript
Retry transient errors
Retry429 and 5xx responses with exponential backoff. Do not retry 400 or 401: they will
fail again until you fix the request or the key.
Common cases
Invalid API key (401, authentication_error)
Invalid API key (401, authentication_error)
Check the key in the dashboard, confirm the
Bearer prefix,
and check for stray whitespace.Unknown model (400, model_not_found)
Unknown model (400, model_not_found)
The
model id is not available. List valid ids with GET /v1/models.
Note that model ids are prefixed, for example openai/gpt-5.Unsupported parameter (400, invalid_request_error)
Unsupported parameter (400, invalid_request_error)
Some models reject a parameter. For example, newer OpenAI models (GPT-5, o3, o4) require
max_completion_tokens instead of max_tokens. Check the model’s supported_parameters in
/v1/models.Rate limited (429)
Rate limited (429)
Back off and retry with exponential backoff, and reduce your request rate.
Upstream unavailable (502, upstream_error)
Upstream unavailable (502, upstream_error)
Best practices
- Branch on the HTTP status and the
error.type, not on the message text. - Retry only transient errors (
429,5xx) with backoff. - Keep keys in environment variables and never log them.