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.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"),
)
response = client.responses.create(
model="gpt-5.2",
instructions="You are a coding assistant that talks like a pirate.",
input="How do I check if a Python object is an instance of a class?",
)
print(response.output_text)Related Repositories
Discover similar tools and frameworks used by developers
Heretic
Tool that removes safety alignment from transformer language models using directional ablation without post-training.
Continue
Multi-LLM coding agent with interactive and automated modes.
Transformers
Unified API for pre-trained transformer models across frameworks.
X Recommendation Algorithm
Open source implementation of X's recommendation algorithm for timeline and notification ranking.
Wan2.1
Diffusion transformer models for text and image-to-video generation.