Python library for calling the OpenAI REST API from Python 3.10+ apps. It includes typed request and response models and sync or async HTTP clients.
Overview
The OpenAI Python library gives applications a structured way to call OpenAI’s HTTP APIs. It ships type definitions for parameters and responses and uses HTTPX-based synchronous and asynchronous clients generated from the project’s OpenAPI description.
Call patterns cover the Responses API for text generation, the older Chat Completions API, vision inputs from URLs or base64 data, streaming via server-sent events, file uploads, pagination helpers, and webhooks with optional signature verification.
The Realtime API path supports low-latency text and audio sessions over WebSockets, including function calling. Clients can use API keys, workload identity from Kubernetes, Azure, or Google Cloud, and optional X.509 mutual TLS modes for automated environments.
The SDK handles retries on certain errors, configurable timeouts, structured error types, and request ID capture for debugging failed calls.
Key features
- Sync and async API clients
- Responses and Chat Completions APIs
- Streaming with server-sent events
- Realtime WebSocket sessions
- Workload identity authentication
- Webhook signature verification
- Auto-pagination for list endpoints
- Typed Pydantic response models
Use cases
- Backend apps calling OpenAI models
- Streaming chat or completion flows
- Realtime voice or multimodal clients
- Automated jobs with cloud identity auth