Requirements
Before installing the SDK, ensure you have:- Python 3.9 or higher - Check your version with
python --version - pip - Python’s package installer (comes with Python)
- OpenAI API key - Get one here
Basic Installation
Install the OpenAI SDK from PyPI using pip:httpx- HTTP client for API requestspydantic- Data validation and type definitionstyping-extensions- Type hint extensionsanyio- Async I/O supportdistro- OS distribution detectiontqdm- Progress bar supportjiter- Fast JSON parsing
Optional Dependencies
The SDK provides several optional dependency groups for extended functionality:Realtime API
For low-latency WebSocket connections with the Realtime API:websockets(>= 13, < 16) - WebSocket client for realtime connections
The Realtime API enables multi-modal conversational experiences with text and audio support.
Data Libraries
For working with data analysis and pandas DataFrames:numpy(>= 1) - Numerical computingpandas(>= 1.2.3) - Data manipulationpandas-stubs(>= 1.1.0.11) - Type stubs for pandas
Voice Helpers
For audio input/output with the Realtime API:sounddevice(>= 0.5.1) - Audio I/Onumpy(>= 2.0.2) - Audio data processing
Aiohttp (Async Performance)
For improved async performance withaiohttp as the HTTP backend:
aiohttp- Alternative async HTTP clienthttpx_aiohttp(>= 0.1.9) - Aiohttp integration for httpx
DefaultAioHttpClient():
Install Multiple Extensions
You can combine multiple optional dependencies:Environment Setup
Get your API key
Sign up for an OpenAI account and get your API key from the API keys page.
Alternative: Use python-dotenv
For better security, use python-dotenv to load your API key from a Create a Load it in your Python code:
.env file:.env file in your project root:.env
Additional Environment Variables
The SDK supports several optional environment variables:| Variable | Description | Default |
|---|---|---|
OPENAI_API_KEY | Your OpenAI API key | Required |
OPENAI_BASE_URL | Custom API endpoint | https://api.openai.com/v1 |
OPENAI_ORG_ID | Organization ID | None |
OPENAI_PROJECT_ID | Project ID | None |
OPENAI_WEBHOOK_SECRET | Webhook verification secret | None |
OPENAI_LOG | Enable logging (info or debug) | Disabled |
You can also pass these as parameters when creating the client instead of using environment variables.
Azure OpenAI
To use the SDK with Azure OpenAI, use theAzureOpenAI class:
AZURE_OPENAI_API_KEYAZURE_OPENAI_ENDPOINTOPENAI_API_VERSIONAZURE_OPENAI_AD_TOKEN
Upgrading
To upgrade to the latest version:Next Steps
Quickstart
Make your first API call with the OpenAI SDK