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:Per-Request Timeout
Override the timeout for specific requests:Granular Timeout Configuration
For fine-grained control, usehttpx.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 raisesAPITimeoutError:
Timeout errors are automatically retried by default. See Retries for configuration.
Async Timeouts
Timeouts work identically withAsyncOpenAI:
Custom HTTP Client Timeout
When providing a custom HTTP client, the SDK uses the client’s timeout if not explicitly set:Timeout Headers
The SDK includes the read timeout in request headers:Best Practices
Common Scenarios
Short Requests
Long Completions
File Uploads
Streaming
Debugging Timeouts
Enable debug logging to see timeout information:Related
- Retries - Automatic retry configuration (timeouts are retried)
- Error Handling - Handling APITimeoutError
- Client Initialization - Client configuration options
- Streaming - Timeout configuration for streaming