What Is the OpenAI Python Library?
The official Python client for the OpenAI API — chat completions, embeddings, streaming, and function calling — and how it fits with Instructor and Pydantic AI
The OpenAI Python library is the official client for the OpenAI API — chat completions, embeddings, image generation, and more, with streaming and function/tool calling built in, so you’re not hand-rolling HTTP requests and response parsing yourself.
What it actually provides
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Explain generators in Python"}],
)
print(response.choices[0].message.content)
The OpenAI Python SDK wraps the API’s endpoints — chat completions, embeddings, images, the Assistants API — in a typed Python client, handling authentication, retries, and response parsing into Python objects rather than raw JSON you’d parse by hand.
Sync and async support
The library ships both a synchronous OpenAI client and an async AsyncOpenAI client with the same interface, so it drops directly into an async application (a FastAPI backend, an async agent loop) without needing a separate async wrapper.
Where it sits relative to higher-level tools
The raw library gives you back whatever the model returns — usually plain text, or a JSON string if you asked the model to produce JSON, which you’d still need to parse and validate yourself. Instructor wraps the OpenAI library (and other providers) specifically to extract typed, validated Pydantic objects directly from a model’s response, removing that manual parsing step. Framework-level tools like LangChain and Pydantic AI frequently use the OpenAI library internally as one of several supported model providers, adding their own abstractions (chains, agents, structured outputs) on top.
When to use it directly
Reach for the OpenAI library directly when you don’t need a higher-level framework’s other features — no multi-step agent logic, no need to swap providers easily, just straightforward calls to OpenAI’s API. Once you need structured output validation, multi-provider support, or agent orchestration, Instructor, Pydantic AI, or LangChain typically save more work than building those patterns by hand on top of the raw client.
Frequently Asked Questions
What is the OpenAI Python library used for?
Calling the OpenAI API from Python — chat completions, embeddings, image generation, and the Assistants API — with built-in support for streaming responses and function/tool calling, without hand-rolling HTTP requests yourself.
Does the OpenAI library support async code?
Yes — it ships both a synchronous client and an async client (AsyncOpenAI), so it fits directly into an async application like a FastAPI backend without needing a separate wrapper.
What’s the difference between the OpenAI library and Instructor?
The OpenAI library is the raw API client — you get back whatever the model returns, typically plain text or a loosely-structured JSON string you’d parse yourself. Instructor wraps it (and other providers) to extract typed, validated Pydantic objects directly, removing manual JSON parsing.
Do you need the OpenAI library if you’re using LangChain or Pydantic AI?
Often it’s used underneath — LangChain and Pydantic AI both support OpenAI as a model provider, frequently calling the OpenAI library internally. You’d use it directly only if you don’t need a higher-level framework’s other abstractions.