Network calls fail; handling failures well is the difference between a demo and a service.
Any API call can fail. The SDK raises a specific exception for each kind of problem, all subclasses of openai.APIError:
BadRequestError(400): the request is invalid. Fix it; retrying won't help.AuthenticationError(401): missing or wrong key.PermissionDeniedError(403): no access.NotFoundError(404): for example a model id that doesn't exist for you.RateLimitError(429): too many requests or tokens, or your quota is used up.InternalServerError(500 and above, including 503 overloaded): a temporary problem at OpenAI.APIConnectionErrorandAPITimeoutError: the request didn't complete.
HTTP errors share openai.APIStatusError, which has status_code and response.
The SDK retries connection problems, 408, 409, 429 and 5xx errors twice by default with exponential backoff (waiting longer after each failure). Change it with OpenAI(max_retries=...) or per call with client.with_options(max_retries=5).
The default timeout is 10 minutes. A chat UI probably wants far less; batch-style work can use more. Set it with OpenAI(timeout=...) or with_options(timeout=...).
Not every 429 is temporary: "you exceeded your current quota" means billing or a spending limit, and no amount of retrying fixes it. Log the error message and alert a person.
Example
import openai
from openai import OpenAI
client = OpenAI(max_retries=3, timeout=60.0)
try:
r = client.responses.create(model="gpt-6-astra", input="Hello")
print(r.output_text)
except openai.BadRequestError as e: # fix the request
print("Invalid request:", e.message)
except openai.RateLimitError as e: # SDK already retried
print("Rate limited or out of quota:", e.message)
except openai.APIConnectionError:
print("Network problem")
except openai.APIStatusError as e: # any other HTTP error
print(e.status_code, e.response.headers.get("x-request-id"))Remember
- Exceptions map to HTTP codes; catch specific ones first
- Two automatic retries by default; tune max_retries and timeout
- Quota 429s are not fixed by retrying
Watch out for
- Adding your own tight retry loop on top of the SDK's retries, multiplying traffic during an outage.