The OpenAI Python library is the official client for the OpenAI REST API, generated from the OpenAPI specification and usable from any Python 3.10+ application. It ships type definitions for every request parameter and response field, and offers a synchronous OpenAI client and an asynchronous AsyncOpenAI client with identical surfaces, both built on HTTPX2.
The primary interface is the Responses API (client.responses.create), with the Chat Completions API supported indefinitely as the earlier standard. Requests take TypedDict parameters; responses come back as Pydantic models with tojson and todict helpers, so editors can autocomplete fields and type checkers can flag errors. Streaming uses server-sent events, list endpoints return auto-paginating iterators, and file uploads accept paths, bytes, or file objects.
Authentication supports API keys and workload identity with short-lived tokens from Kubernetes service accounts, Azure managed identity, Google Cloud compute metadata, a custom JWT provider, or X.509 mutual TLS certificates. Separate client classes and configuration cover Azure OpenAI and Amazon Bedrock's OpenAI-compatible API, the latter with SigV4 signing through the standard AWS credential chain.
Features
- Sync and async clients: OpenAI and AsyncOpenAI expose the same methods; the async client can swap in aiohttp as its transport
- Responses and Chat Completions: first-class support for both text-generation APIs, plus vision inputs by image URL or base64
- Typed requests and responses: TypedDict parameters and Pydantic response models with JSON and dict serialization
- Streaming: server-sent event streams for incremental output
- Realtime API: WebSocket sessions for low-latency text and audio conversations with function calling
- Pagination: list responses iterate across pages automatically
- Workload identity: token providers for Kubernetes, Azure managed identity, GCP metadata, custom JWTs, and X.509 mTLS with configurable refresh buffers
- Webhook verification: helpers that parse and verify webhook payload signatures
- Retries and timeouts: connection errors, 408, 409, 429, and 5xx responses retry twice by default with exponential backoff; max_retries and timeouts are configurable per client or per request
- Request inspection: access to request IDs, raw responses and headers, streaming response bodies, and undocumented endpoints or parameters
- Azure and Bedrock: an AzureOpenAI class and a Bedrock provider configuration for OpenAI-compatible endpoints
- Custom HTTP client: proxy, transport, and mutual TLS configuration through an HTTPX2 client instance