Skip to main content
This guide will help you install the OpenAI Python SDK and set up your development environment.

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:
This installs the core SDK with all essential dependencies:
  • httpx - HTTP client for API requests
  • pydantic - Data validation and type definitions
  • typing-extensions - Type hint extensions
  • anyio - Async I/O support
  • distro - OS distribution detection
  • tqdm - Progress bar support
  • jiter - 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:
This adds:
  • 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:
This adds:
  • numpy (>= 1) - Numerical computing
  • pandas (>= 1.2.3) - Data manipulation
  • pandas-stubs (>= 1.1.0.11) - Type stubs for pandas

Voice Helpers

For audio input/output with the Realtime API:
This adds:
  • sounddevice (>= 0.5.1) - Audio I/O
  • numpy (>= 2.0.2) - Audio data processing
Voice helpers are particularly useful when building interactive voice applications with the Realtime API.

Aiohttp (Async Performance)

For improved async performance with aiohttp as the HTTP backend:
This adds:
  • aiohttp - Alternative async HTTP client
  • httpx_aiohttp (>= 0.1.9) - Aiohttp integration for httpx
To use aiohttp, instantiate the async client with DefaultAioHttpClient():

Install Multiple Extensions

You can combine multiple optional dependencies:

Environment Setup

1

Get your API key

Sign up for an OpenAI account and get your API key from the API keys page.
2

Set environment variable

Set the OPENAI_API_KEY environment variable:
Add this to your shell profile (.bashrc, .zshrc, etc.) to make it permanent.
3

Alternative: Use python-dotenv

For better security, use python-dotenv to load your API key from a .env file:
Create a .env file in your project root:
.env
Load it in your Python code:
Never commit your .env file to version control. Add it to .gitignore.
4

Verify installation

Test your installation:

Additional Environment Variables

The SDK supports several optional environment variables:
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 the AzureOpenAI class:
Azure-specific environment variables:
  • AZURE_OPENAI_API_KEY
  • AZURE_OPENAI_ENDPOINT
  • OPENAI_API_VERSION
  • AZURE_OPENAI_AD_TOKEN

Upgrading

To upgrade to the latest version:
Check your installed version:

Next Steps

Quickstart

Make your first API call with the OpenAI SDK