OpenAI Python Library
Official Python SDK to call OpenAI APIs including GPT models and embeddings.
Model SDKsPYPI
3. Used by agents' projects
Dependency in 248 public GitHub projects set up for AI coding agents that we scanned (4.0%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
From the package registry and GitHub, checked daily. Tap a verdict to see its source.How we checkedReport an error
What OpenAI Python Library does
This is OpenAI's own Python client, generated from its OpenAPI spec, with matching sync and async clients and built-in workload identity auth, so cloud jobs can call the API with short-lived tokens instead of a long-lived API key.
OpenAI Python Library popularity
Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated
How OpenAI Python Library is used
- OpenAI Python Library is a dependency in 248 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (15.3% of the 1,623 Python ones), #3 of 92 in model sdks.
- Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 3,165; those are left out of every share on this page.
- The most used alternative in our scan is Anthropic Python SDK (115 projects).
- Projects that use it most often also use python-dotenv (70.2%), Pydantic (56.5%), Uvicorn (51.6%).
- It is more common in projects set up for OpenClaw (10.8%) than for OpenCode (2.9%).
By coding agent
Often used with
OpenAI Python Library in practice
Strengths
Instead of an API key, the client can get short-lived tokens from Kubernetes service account tokens, Azure managed identity, GCP compute engine metadata, a custom JWT provider, or X.509 mutual TLS. You pass a <code>workload_identity</code> setting when you build the client.
For workload identity, the README gives a default token refresh buffer of 1200 seconds (20 minutes) before expiration. You can change it with <code>refresh_buffer_seconds</code>.
Because the library is generated from OpenAI's OpenAPI specification, nested request parameters are <code>TypedDict</code>s. Responses are Pydantic models with <code>to_json()</code> and <code>to_dict()</code> helpers.
The streaming and file upload sections both say the async client uses the same interface as the sync one. For async uploads, a <code>PathLike</code> file is read asynchronously automatically.
Watch out for
In the Realtime API, an error arrives as an <code>error</code> event and the connection stays open. The README says the SDK raises no exception for it, so your event loop has to handle it.
The README says mutual-TLS workload identity currently supports HTTP APIs. Realtime and WebSockets are not included.
Identity settings are read when the client is built, so changing identity means a new client. Azure clients do not support X.509 workload identity.
Generate text with the Responses API
import os
from openai import OpenAI
client = OpenAI(
# This is the default and can be omitted
api_key=os.environ.get("OPENAI_API_KEY"),
)
response = client.responses.create(
model="gpt-5.5",
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)More about OpenAI Python Library
Agent Skills for it
- : Skills Catalog for Codex
- openai-docs: Use when the user asks how to build with OpenAI products or APIs and needs up-to-date official documentation with citations (for example: Co
- openai-tts: OpenAI Text-to-Speech API for high-quality speech synthesis. Use for generating natural-sounding audio from text with customizable voices an
- openai: Check OpenAI API access and list the models your key can use. Read-only. Trigger phrases: openai, openai models, what models do I have acces
- openai-frontend-design: This skill should be used when the user asks for "visual concepts", "image-led website design", or "concept fidelity". Create visual concept
Head to head
- OpenAI Python Library vs Anthropic Python SDK used together in 65 projects
- OpenAI Python Library vs Google GenAI Python SDK used together in 42 projects
Packages around it
- python-dotenv: Loads .env files
- pydantic: Data models and validation
- uvicorn: ASGI server
- fastapi: Web API framework
- requests: HTTP client
- numpy: Numeric arrays
Alternatives
OpenAI Python Library This page
Official Python library for calling Claude via the Anthropic API.
Official Python SDK for Google's generative AI models including Gemini.
Official Python client SDK for Alibaba DashScope API to call language models.
Official Python SDK for the Mistral AI API to call Mistral language models.
Official SDK for accessing Cohere's language models and APIs.
Get started
Install it.
pip install openaiRules for your agent
## OpenAI Python Library
- Prefer loading `OPENAI_API_KEY` from the environment over passing `api_key`. The README recommends python-dotenv and a `.env` file so the key is not stored in source control.
- Use `client.responses.create` for new code. The README calls the Responses API the primary API and Chat Completions the previous standard.
- In async code, use `AsyncOpenAI` and `await` every call. Use `async for` when streaming or paginating.
- Iterate list calls directly instead of writing manual page loops. Use `.has_next_page()` and `.get_next_page()` only when you need page-level control.
- In Realtime connections, check `event.type == 'error'` inside the event loop. The SDK does not raise on error events.
- When uploading `io.BytesIO`, pass a `(filename, contents, media type)` tuple so the API can detect the format.
- With X.509 workload identity, build a new client to change identity. X.509 currently covers HTTP APIs, not Realtime or WebSockets.