Skip to main content

OpenAI Python SDK

The official Python library for the OpenAI API. Build powerful AI applications with type-safe, async-ready client libraries.

Quick Start

Get up and running with the OpenAI Python SDK in minutes

1

Install the package

Install the OpenAI Python library using pip:
pip install openai
2

Set your API key

Export your OpenAI API key as an environment variable:
export OPENAI_API_KEY='your-api-key-here'
Get your API key from the OpenAI Platform
3

Make your first API call

Create a simple script to generate text using the Responses API:
from openai import OpenAI

client = OpenAI()

response = client.responses.create(
    model="gpt-5.2",
    instructions="You are a helpful assistant.",
    input="Explain the theory of relativity in simple terms."
)

print(response.output_text)

Explore the SDK

Discover the powerful features of the OpenAI Python SDK

Responses API

Generate text with the modern Responses API

Chat Completions

Create conversational AI experiences

Audio

Convert text to speech and transcribe audio

Images

Generate and edit images with AI

Embeddings

Create vector representations of text

Assistants

Build stateful AI assistants with threads

Core Features

Everything you need to build production-ready AI applications

Type Safety

Full type definitions for all request and response fields

Async Support

Native async/await support for high-performance applications

Streaming

Stream responses in real-time for better UX

Error Handling

Comprehensive error types and retry logic

Azure OpenAI

Seamless integration with Azure OpenAI Service

Helpers

Utility functions and event handlers

Ready to build with OpenAI?

Start building powerful AI applications with the OpenAI Python SDK today.

Get Started

Build docs developers (and LLMs) love