Skip to content

OpenAI Python SDK

OpenAI Python SDK is OpenAI’s official open-source Python library for calling the OpenAI API, installed from PyPI as openai and used to send prompts to OpenAI models and read back their responses.

Programs create an OpenAI client, which reads a key from the OPENAI_API_KEY environment variable, and call methods that mirror the REST API’s endpoints. The Responses API, reached through client.responses.create(), is the primary interface. The older Chat Completions API, called through client.chat.completions.create(), stays supported indefinitely.

Every request parameter and response field is typed, and responses come back as Pydantic models, so editors can autocomplete them. The library streams output as server-sent events, retries failed requests twice by default, and supports structured output and function calling. An AsyncOpenAI client exposes the same methods to asyncio code, and AzureOpenAI targets Azure deployments.

Many providers expose OpenAI-compatible endpoints, so the base_url parameter can point the same client at a local Ollama server, a gateway like OpenRouter, or another vendor’s models. That makes the package a common default in Python AI tutorials even when the model doesn’t come from OpenAI.

The SDK is generated from OpenAI’s OpenAPI specification, is Apache-2.0 licensed, and needs Python 3.10 or later. OpenAI ships its Agents SDK separately as openai-agents. Comparable clients include the Anthropic SDK and Google’s google-genai, while LiteLLM wraps many providers behind one interface.

Official website: developers.openai.com/api/docs/libraries

How to Integrate ChatGPT's API With Python Projects

Tutorial

How to Integrate ChatGPT's API With Python Projects

Learn how to use the ChatGPT Python API with the openai library to build AI-powered features in your Python applications.

intermediate ai api

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Martin Breuss • Updated Nov. 5, 2026