async/await.
Constructor
Parameters
Your OpenAI API key. If not provided, the client will attempt to read it from the
OPENAI_API_KEY environment variable. Required.Can also be an async callable that returns a string, which will be called to refresh the API key when needed.Your OpenAI organization ID. If not provided, the client will attempt to read it from the
OPENAI_ORG_ID environment variable.When provided, this will be sent as the OpenAI-Organization header on all requests.Your OpenAI project ID. If not provided, the client will attempt to read it from the
OPENAI_PROJECT_ID environment variable.When provided, this will be sent as the OpenAI-Project header on all requests.Secret key for validating webhook signatures. If not provided, the client will attempt to read it from the
OPENAI_WEBHOOK_SECRET environment variable.Override the default base URL for the API. If not provided, the client will attempt to read it from the
OPENAI_BASE_URL environment variable, otherwise defaults to https://api.openai.com/v1.Base URL for WebSocket connections (used for Realtime API).If not specified, the default base URL will be used with the scheme changed to
wss://. For example, https://api.openai.com becomes wss://api.openai.com.Request timeout configuration. Can be:
- A float representing timeout in seconds
- An
httpx.Timeoutobject for fine-grained control Noneto disable timeouts
Maximum number of retries for failed requests. Set to
0 to disable retries.The client will automatically retry requests that fail due to network errors or certain HTTP status codes (429, 500, 502, 503, 504).Additional HTTP headers to include on all requests.
Default query parameters to include on all requests.
Custom
httpx.AsyncClient instance to use for making requests.You can use DefaultAsyncHttpxClient to retain the default values for limits, timeout, and follow_redirects while customizing other options.Properties
The client provides access to various API resources through properties:client.chat- Chat completionsclient.completions- Text completionsclient.embeddings- Embeddingsclient.files- File operationsclient.images- Image generationclient.audio- Audio transcription and generationclient.moderations- Content moderationclient.models- Model informationclient.fine_tuning- Fine-tuning operationsclient.batches- Batch APIclient.uploads- Multipart file uploadsclient.beta- Beta features (Assistants, Threads, etc.)client.vector_stores- Vector store operationsclient.realtime- Realtime APIclient.webhooks- Webhook utilities
Examples
Basic usage
Streaming responses
Concurrent requests
Custom timeout configuration
Custom retry configuration
Custom HTTP client with connection pooling
Using an async callable for dynamic API key
Context manager for automatic cleanup
Using with_options for per-request configuration
Methods
copy()
Create a new client instance with the same configuration, optionally overriding specific options.with_options()
Alias forcopy(), useful for inline usage:
Differences from Sync Client
TheAsyncOpenAI client is functionally identical to the synchronous OpenAI client, with the following key differences:
- All API methods are async - Use
awaitwhen calling API methods - Async HTTP client - Accepts
httpx.AsyncClientinstead ofhttpx.Client - Async API key callable - The
api_keyparameter can be an async function returningAwaitable[str] - AsyncStream - Streaming responses return
AsyncStreamobjects that are iterated withasync for - Context manager support - Can be used with
async withfor automatic cleanup