Skip to main content
The OpenAI Python library provides convenient access to the OpenAI REST API from any Python 3.9+ application. The library includes type definitions for all request params and response fields, and offers both synchronous and asynchronous clients powered by httpx.

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 OpenAI and asynchronous AsyncOpenAI clients
  • 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:
from openai import OpenAI

client = OpenAI()

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)

Chat Completions API

The previous standard for generating text, supported indefinitely:
from openai import OpenAI

client = OpenAI()

completion = client.chat.completions.create(
    model="gpt-5.2",
    messages=[
        {"role": "developer", "content": "Talk like a pirate."},
        {"role": "user", "content": "How do I check if a Python object is an instance of a class?"},
    ],
)

print(completion.choices[0].message.content)

Vision Support

Process images alongside text for multi-modal interactions:
response = client.responses.create(
    model="gpt-5.2",
    input=[
        {
            "role": "user",
            "content": [
                {"type": "input_text", "text": "What is in this image?"},
                {"type": "input_image", "image_url": "https://example.com/image.jpg"},
            ],
        }
    ],
)

Async Support

Use AsyncOpenAI for asynchronous operations:
import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI()

async def main():
    response = await client.responses.create(
        model="gpt-5.2",
        input="Explain quantum computing to a five year old."
    )
    print(response.output_text)

asyncio.run(main())

Streaming Responses

Stream responses in real-time using Server-Sent Events:
stream = client.responses.create(
    model="gpt-5.2",
    input="Write a short story about a robot.",
    stream=True,
)

for event in stream:
    print(event)

Realtime API

Build low-latency, multi-modal conversational experiences with WebSocket support:
import asyncio
from openai import AsyncOpenAI

async def main():
    client = AsyncOpenAI()

    async with client.realtime.connect(model="gpt-realtime") as connection:
        await connection.session.update(
            session={"type": "realtime", "output_modalities": ["text"]}
        )

        await connection.conversation.item.create(
            item={
                "type": "message",
                "role": "user",
                "content": [{"type": "input_text", "text": "Say hello!"}],
            }
        )
        await connection.response.create()

        async for event in connection:
            if event.type == "response.output_text.delta":
                print(event.delta, flush=True, end="")
            elif event.type == "response.done":
                break

asyncio.run(main())

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

Next Steps

Installation

Install the SDK and set up your environment

Quickstart

Make your first API call in minutes

Build docs developers (and LLMs) love