openai-python: Official Python library for OpenAI API
Type-safe Python client for OpenAI's REST API.
Learn more about openai-python
The openai-python library is the official Python client for interacting with OpenAI's REST API, providing type-safe bindings for all API endpoints including chat completions, embeddings, image generation, and audio processing. It implements a synchronous and asynchronous HTTP client architecture that handles request serialization, authentication via API keys, automatic retries with exponential backoff, and response deserialization into strongly-typed Python objects. The library supports multimodal interactions by encoding various input types such as text prompts, base64-encoded images, and file uploads into properly formatted API request payloads that conform to OpenAI's JSON schema specifications. It provides Pydantic-based model validation for request parameters and response objects, enabling IDE autocompletion and compile-time type checking while maintaining backwards compatibility with OpenAI's evolving API surface.

Generated from OpenAPI spec
The library is automatically generated from OpenAI's OpenAPI specification using Stainless, ensuring consistency with the API and reducing manual maintenance of endpoint definitions.
Dual sync and async clients
Provides both synchronous and asynchronous client implementations powered by httpx, with optional aiohttp backend for applications requiring higher concurrency throughput.
Complete type coverage
Includes comprehensive type definitions for all request parameters and response fields, enabling IDE autocompletion and static type checking across the entire API surface.
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "developer", "content": "Talk like a pirate."},
{
"role": "user",
"content": "How do I check if a Python object is an instance of a class?",
},
],
)
print(completion.choices[0].message.content)Drops Python 3.8 support and adds Python 3.14 compatibility; upgrade your runtime to Python 3.9+ before installing.
- –Upgrade to Python 3.9 or later, as Python 3.8 is no longer supported.
- –Python 3.14 compatibility is now included via a bug fix.
Patch release correcting the nullability of logprobs in API response types; no breaking changes or migration required.
- –Update type definitions to allow null values for logprobs fields, fixing type-checking errors in strict environments.
- –No action required unless you experienced type errors with logprobs; upgrade to resolve those issues.
Adds Realtime API token limits and hybrid search ranking; removes InputAudio from ResponseInputContent (potential breaking change).
- –Remove InputAudio from ResponseInputContent may break code relying on that type in Realtime API responses.
- –Upgrade to fix stream and file handle leaks that could exhaust resources in long-running applications.
See how people are using openai-python
Related Repositories
Discover similar tools and frameworks used by developers
xformers
Memory-efficient PyTorch components for transformer architectures.
pytorch
Python framework for differentiable tensor computation and deep learning.
AI-Trader
LLM agent benchmarking framework for autonomous market trading.
dinov2
PyTorch vision transformers pretrained on 142M unlabeled images.
TTS
PyTorch toolkit for deep learning text-to-speech synthesis.