Errors

Errors

Errors use standard HTTP status codes and the OpenAI error envelope, so the SDKs raise the exception classes you already handle. error.param names the offending field when there is one.

Response
{
  "error": {
    "message": "Invalid API key provided",
    "type": "authentication_error",
    "param": null,
    "code": "invalid_api_key"
  }
}
StatusTypeMeaningWhat to do
400invalid_request_errorA parameter is missing, malformed or outside its range. error.param names it.Fix the request. Retrying will not help.
401authentication_errorMissing, malformed, disabled or unknown API key.Check the Authorization header and the key's status.
402insufficient_quotaThe account has no credit left for this request.Top up. Do not retry — the balance will not change on its own.
403permission_errorThe key or tier may not use this model, endpoint or SDK.Use a model your tier allows, or a key with the right scope.
404not_found_errorNo such model, job or asset.Check the id against GET /v1/models.
408timeout_errorThe generation exceeded the budget for this request.Retry, or ask for a shorter output.
409invalid_request_errorAn Idempotency-Key was reused with a different body, or a job is already in flight.Use a new key, or resend the identical body.
413invalid_request_errorThe upload or request body is over the limit.See file size limits.
422invalid_request_errorThe request is well formed but the values cannot be served together.Read the message; it names the conflict.
429rate_limit_errorRequests per minute, tokens per minute, a daily cap, or too many concurrent requests.Back off for Retry-After seconds. See rate limits.
500server_errorAn unexpected fault in the gateway.Retry with backoff. Quote X-Request-Id if it persists.
501server_errorThe provider adapter does not implement this capability for this model.Use a different model for that modality.
502provider_errorThe upstream provider returned something unusable.Retry; routing will usually pick a different provider.
503service_unavailableNo provider can serve this model right now, or the gateway is shedding load.Retry with backoff, or use another model.
504timeout_errorThe upstream did not answer in time.Retry.

Handling them

import openai

try:
    response = client.chat.completions.create(model="gpt-5.5", messages=msgs)
except openai.RateLimitError as e:
    # 429 — honour Retry-After rather than retrying immediately.
    wait = int(e.response.headers.get("retry-after", 60))
except openai.APIStatusError as e:
    if e.status_code == 402:
        # Out of credits — top up, do not retry.
        ...
    print(e.status_code, e.body["error"]["code"])
Retry 429, 500, 502, 503 and 504. Do not retry 400, 401, 402, 403, 404 or 409. The first group is transient; the second will keep failing until something on your side changes. Errors raised after a stream has started arrive as an error frame inside the stream, because the HTTP status was already sent.
Getting help. Every error response carries X-Request-Id. Quote it and we can find the exact request — without it, we cannot.