Skip to content

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

  1. Set timeouts on purpose; don't rely on library defaults.
  2. Stream long replies so the first tokens arrive early.
  3. Cap max output tokens so replies can't run on.
  4. Retry timeouts in one layer only, and count each retry's input cost.
  5. Move very long jobs to a background queue or batch API.

Related

Sources

All guides · All errors