Skip to main content

Overview

Timeouts prevent requests from hanging indefinitely. The OpenAI Python SDK provides flexible timeout configuration at both the client and request level. Default timeout: 600 seconds (10 minutes)

Default Behavior

By default, all requests timeout after 10 minutes:

Simple Timeout Configuration

Client-Level Timeout

Set a default timeout for all requests:
Setting timeout=None disables timeouts completely. This can cause requests to hang indefinitely if the server doesn’t respond.

Per-Request Timeout

Override the timeout for specific requests:

Granular Timeout Configuration

For fine-grained control, use httpx.Timeout to configure different timeout phases:

Timeout Phases

float
default:"60.0"
Total timeout for the entire request/response cycle. This is the maximum time from starting the request to receiving the complete response.
float
default:"5.0"
Maximum time to establish a connection to the server.
float
default:"30.0"
Maximum time to wait for data to be received from the server. This applies per read operation, not the entire response.
float
default:"10.0"
Maximum time to wait for data to be sent to the server. This applies per write operation.
float
default:"5.0"
Maximum time to wait for a connection from the connection pool.

Example Configurations

Fast Requests

Long-Running Requests

Streaming Requests

For streaming requests, set timeout=None and rely on read timeout for per-chunk timing.

Timeout Errors

When a timeout occurs, the SDK raises APITimeoutError:
Timeout errors are automatically retried by default. See Retries for configuration.

Async Timeouts

Timeouts work identically with AsyncOpenAI:

Custom HTTP Client Timeout

When providing a custom HTTP client, the SDK uses the client’s timeout if not explicitly set:
When using a custom http_client, prefer DefaultHttpxClient to preserve SDK defaults.

Timeout Headers

The SDK includes the read timeout in request headers:
This helps with server-side debugging and timeout analysis.

Best Practices

  • Set reasonable timeouts - Balance between too short (frequent failures) and too long (poor UX)
  • Use per-request timeouts for requests with different expected durations
  • Configure streaming timeouts with timeout=None and appropriate read timeout
  • Monitor timeout errors - Frequent timeouts may indicate performance issues
  • Consider retry behavior - Timeouts are retried by default, increasing total wait time

Common Scenarios

Short Requests

Long Completions

File Uploads

Streaming

Debugging Timeouts

Enable debug logging to see timeout information: