What Is the OpenAI Python Library?

The official Python client for the OpenAI API: Responses, Chat Completions, embeddings and streaming, and how it fits with Instructor and Pydantic AI

The OpenAI Python library is the official client for the OpenAI API: the Responses API, Chat Completions, embeddings, image generation and more, with streaming and tool calling built in, so you’re not hand-rolling HTTP requests and response parsing yourself. It is at version 3 (3.51.0 on 14 November 2026), so tutorials written for 1.x or 2.x may not match exactly.

What it actually provides

from openai import OpenAI

client = OpenAI()  # reads OPENAI_API_KEY from the environment
response = client.responses.create(
    model="gpt-6-astra",  # use a current model ID; see the models page
    input="Explain generators in Python",
)
print(response.output_text)

OpenAI’s documentation recommends the Responses API for all new projects. Chat Completions (client.chat.completions.create(model=..., messages=[...])) remains supported and is what most older tutorials and compatible third-party servers use. Both exist on the 3.51.0 client; The Chat Completions request path was confirmed and response parsing offline against a mock transport; the Responses example and model ID come from OpenAI’s quickstart. Model IDs retire quickly: the gpt-4 ID used in many tutorials, has a shutdown date of 23 October 2026 and will error.

The OpenAI Python SDK wraps the API’s endpoints in a typed Python client, handling authentication, retries, and response parsing into Python objects rather than raw JSON you’d parse by hand. The Assistants API, which older versions of this page listed, was shut down on 26 August 2026; OpenAI points users to the Responses and Conversations APIs.

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 support OpenAI as one of several model providers, adding their own abstractions (agents, structured outputs) on top. Many other providers, including Ollama, expose an OpenAI-compatible endpoint, so this client doubles as a way to talk to them by changing base_url.

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: the Responses API (OpenAI’s recommended API for new projects), Chat Completions, embeddings and image generation, with built-in support for streaming and tool calling, without hand-rolling HTTP requests. The Assistants API was shut down on 26 August 2026.

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 and typically depend on the OpenAI library for it. You’d use it directly only if you don’t need a higher-level framework’s other abstractions.