APITimeoutError: Request timed out.
Cause: The SDK gave up waiting for the API before a response arrived, usually because the reply is long, the model is slow, or the timeout is too short for the job.
Copy-paste fix
Give long jobs a longer timeout on that call only, stream them, and keep short calls on a short timeout.
import openai
client = openai.OpenAI(timeout=30.0, max_retries=2) # defaults for short calls
def long_job(messages):
try:
stream = client.with_options(timeout=300.0, max_retries=1) \
.chat.completions.create(model=MODEL, messages=messages,
max_completion_tokens=4000, stream=True)
return "".join(c.choices[0].delta.content or ""
for c in stream if c.choices)
except openai.APITimeoutError:
# log model, input size and timeout; then fail or queue for later
raise
Compare faster, cheaper models for time-sensitive calls on TokenCenter
What the error means
In openai-python, APITimeoutError is a subclass of APIConnectionError whose message is Request timed out. The client takes timeout and max_retries settings, and with_options() overrides them per call.
OpenAI's error guide says to retry after a brief wait. On the Claude API the matching server-side error is timeout_error, and Anthropic recommends streaming for long requests.
Checklist
- Set timeouts on purpose; don't rely on library defaults.
- Stream long replies so the first tokens arrive early.
- Cap max output tokens so replies can't run on.
- Retry timeouts in one layer only, and count each retry's input cost.
- Move very long jobs to a background queue or batch API.
Related
- peer closed connection (incomplete chunked read)
- finish_reason: "length" (reply cut off)
- Failure mode: Retry and cost multipliers
- Failure mode: Budget surprises
- ModelRaters list: Best for fast and low cost