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:
pip install openai
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:
pip install openai[realtime]
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:
pip install openai[datalib]
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:
pip install openai[voice_helpers]
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:
pip install openai[aiohttp]
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():
import asyncio
from openai import AsyncOpenAI, DefaultAioHttpClient

async def main():
    async with AsyncOpenAI(
        http_client=DefaultAioHttpClient(),
    ) as client:
        response = await client.responses.create(
            model="gpt-5.2",
            input="Say this is a test"
        )
        print(response.output_text)

asyncio.run(main())

Install Multiple Extensions

You can combine multiple optional dependencies:
pip install openai[realtime,voice_helpers]

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:
export OPENAI_API_KEY="sk-proj-..."
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:
pip install python-dotenv
Create a .env file in your project root:
.env
OPENAI_API_KEY=sk-proj-...
Load it in your Python code:
from dotenv import load_dotenv
from openai import OpenAI

load_dotenv()  # Load .env file
client = OpenAI()  # API key automatically loaded from environment
Never commit your .env file to version control. Add it to .gitignore.
4

Verify installation

Test your installation:
import openai
from openai import OpenAI

print(f"OpenAI SDK version: {openai.__version__}")

client = OpenAI()
print("✓ SDK installed and configured successfully!")

Additional Environment Variables

The SDK supports several optional environment variables:
VariableDescriptionDefault
OPENAI_API_KEYYour OpenAI API keyRequired
OPENAI_BASE_URLCustom API endpointhttps://api.openai.com/v1
OPENAI_ORG_IDOrganization IDNone
OPENAI_PROJECT_IDProject IDNone
OPENAI_WEBHOOK_SECRETWebhook verification secretNone
OPENAI_LOGEnable 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 the AzureOpenAI class:
from openai import AzureOpenAI

client = AzureOpenAI(
    api_version="2023-07-01-preview",
    azure_endpoint="https://example-endpoint.openai.azure.com",
)

completion = client.chat.completions.create(
    model="deployment-name",
    messages=[{"role": "user", "content": "Hello!"}],
)
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:
pip install --upgrade openai
Check your installed version:
import openai
print(openai.__version__)

Next Steps

Quickstart

Make your first API call with the OpenAI SDK

Build docs developers (and LLMs) love