What is the OpenAI Python SDK?
The OpenAI Python SDK is the official Python library for interacting with OpenAI’s API. It simplifies the process of building AI-powered applications by providing:- Type-safe interfaces - Complete type definitions using TypedDicts and Pydantic models
- Sync and async support - Choose between synchronous
OpenAIand asynchronousAsyncOpenAIclients - Automatic retries - Built-in retry logic with exponential backoff for failed requests
- Streaming responses - Server-sent events (SSE) support for real-time streaming
- Pagination helpers - Auto-paginating iterators for list endpoints
- Error handling - Comprehensive exception types for different error scenarios
Key Features
Responses API
The primary API for interacting with OpenAI models. Generate text, handle conversations, and process multi-modal inputs:Chat Completions API
The previous standard for generating text, supported indefinitely:Vision Support
Process images alongside text for multi-modal interactions:Async Support
UseAsyncOpenAI for asynchronous operations:
Streaming Responses
Stream responses in real-time using Server-Sent Events:Realtime API
Build low-latency, multi-modal conversational experiences with WebSocket support:API Resources
The SDK provides access to the following OpenAI API resources:Responses
Primary API for generating text and handling conversations
Chat Completions
Generate chat-based completions with message history
Embeddings
Create vector embeddings for text
Images
Generate and edit images with DALL·E
Audio
Speech-to-text and text-to-speech
Files
Upload and manage files
Fine-tuning
Create custom models with your data
Batches
Process async batch requests
Realtime
Low-latency multi-modal conversations
Moderations
Classify content for safety
Requirements
- Python 3.9 or higher
- OpenAI API key (get one here)
Next Steps
Installation
Install the SDK and set up your environment
Quickstart
Make your first API call in minutes