OpenAI Python SDK

The official Python library for the OpenAI REST API, generated from OpenAI's OpenAPI spec with typed sync and async clients.

SDK
PyPI
v3.37.0
31,926 stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
97 /100 Excellent
Development Activity 100
Maintenance 100
Community 88
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
88 /100 Excellent
Architecture 88
Code Quality 92
Innovation 80
Learning Curve 90

The openai package is OpenAI’s official Python client, giving typed, generated access to the full REST API — Responses, Chat Completions, Embeddings, Files, Fine-tuning, Realtime, Vector Stores, and more — from a single OpenAI (or AsyncOpenAI) client object. Every resource ships synchronous and asynchronous variants built on HTTPX2, with Pydantic-modeled request and response objects so editors and type checkers catch mistakes before a request is even sent.

Beyond the base HTTP surface, the library adds the pieces most teams end up rebuilding by hand: automatic retries with backoff on transient errors, request-ID propagation for support tickets, cursor-based pagination helpers, streaming iterators for both the Responses and Chat Completions APIs, WebSocket-based Realtime API support, webhook signature verification, and first-party workload-identity auth (Kubernetes service accounts, Azure managed identity, GCP metadata tokens) alongside plain API keys. Azure OpenAI and Amazon Bedrock are supported through drop-in client subclasses.

What You Get

  • Typed OpenAI and AsyncOpenAI clients covering every REST endpoint, generated directly from OpenAI’s OpenAPI specification
  • Streaming support for the Responses and Chat Completions APIs, plus a dedicated WebSocket client for the Realtime API
  • Built-in retry logic, timeout configuration, request-ID propagation, and cursor-based pagination for list endpoints
  • .with_raw_response and .with_streaming_response accessors for reading headers or streaming bytes without a second client
  • Workload-identity authentication providers for Kubernetes, Azure, and GCP, plus drop-in AzureOpenAI and Bedrock client subclasses
  • Webhook payload parsing and signature verification helpers for handling asynchronous events (e.g. completed fine-tuning jobs)

Common Use Cases

  • Calling GPT models for text generation, chat, and tool/function calling from a Python backend
  • Building an async FastAPI or Django service that streams model output token-by-token to clients
  • Running fine-tuning jobs and managing uploaded training files programmatically
  • Building a realtime voice assistant over the WebSocket-based Realtime API
  • Deploying the same codebase against OpenAI, Azure OpenAI, or Bedrock-hosted models with minimal changes

Under The Hood

Architecture

The public entry point is the OpenAI/AsyncOpenAI class pair in _client.py, which subclasses the shared SyncAPIClient/AsyncAPIClient base in _base_client.py and exposes one namespaced resource object per API surface (chat, responses, embeddings, files, fine_tuning, realtime, vector_stores, and more) mirrored under resources/. Each resource module is mechanically generated from OpenAI’s OpenAPI spec and composes three response variants per endpoint — plain, WithRawResponse, and WithStreamingResponse — via thin wrapper classes, with request/response bodies modeled as Pydantic BaseModel subclasses in _models.py and list endpoints handled by shared cursor/page classes in pagination.py. Streaming goes through a dedicated _streaming.py module plus per-feature streaming helpers under lib/. The generated resource layer is largely boilerplate and low-risk to extend; the base client’s auth, retry, and transport logic is the one piece that would ripple across every resource if changed.

Tech Stack

Built for Python 3.10+ on top of HTTPX2 (a fork/successor of httpx) for transport, Pydantic (supporting both v1 and v2, pinned 1.10–<3) for typed models, anyio for async-runtime abstraction, and jiter for fast JSON decoding. Optional extras add aiohttp as an alternate async transport, websockets for the Realtime API, numpy/pandas for the datalib helpers, sounddevice for voice helpers, and botocore for Bedrock auth. The package is built with hatchling and its dependency graph is locked via uv (uv.lock); CI runs the full pytest, mypy, and ruff suite across the supported Python matrix plus the next CPython prerelease.

Code Quality

The repository ships 159 test_*.py files spanning tests/api_resources, tests/lib, tests/compat, and tests/respx2 fixtures, run with pytest and pytest-asyncio. Source is fully type-annotated (a py.typed marker is present) and checked with mypy in strict mode; ruff enforces lint and formatting in CI. Errors are modeled as an explicit typed exception hierarchy in _exceptions.py rather than surfaced as bare exceptions, and 30 runnable example scripts back the documentation.

What Makes It Unique

Because both sync and async clients, plus Azure and Bedrock variants, are generated from one OpenAPI spec, the library keeps near-total API-surface parity across every access pattern rather than treating async or cloud-vendor support as an afterthought. The .with_raw_response/.with_streaming_response accessor pattern gives typed access to headers or a live stream without instantiating a second client, and pluggable workload-identity token providers (Kubernetes, Azure, GCP, or a custom callback) sit alongside the plain API-key path for teams that need short-lived credentials in managed environments.

Used by 59 apps in this directory

Python
71%
Apache 2.0

GPT Researcher

AI Assistants · Productivity

30,269

The pioneering open-source autonomous AI agent that conducts deep, multi-source research and produces citation-backed reports exceeding 2,000 words — faster and more reliably than any human researcher.

View details
90
Repo Health
91
Technical
64
Dependency
Built with
Python 71%
TypeScript 18%
Updated 2 weeks ago
Python
100%
MIT

Graphify

AI Agents

133,997

A YC-backed, open-source knowledge graph skill for AI coding assistants — type /graphify and it maps your entire project (code, docs, PDFs, images, videos) into a queryable graph instead of grepping through files.

View details
84
Repo Health
73
Technical
72
Dependency
Built with
Python 100%
Updated 1 weeks ago
Go
87%
MIT

Hatchet

AI Development · Automation · Developer Tools

8,174

A Postgres-backed orchestration engine for background tasks, AI agents, and durable workflows that replaces Redis queues and multi-datastore durable execution platforms with a single self-hostable service.

View details
88
Repo Health
83
Technical
67
Dependency
Built with
Go 87%
Updated 1 weeks ago
Python
82%
Apache 2.0

headroom

AI Development · Developer Tools

77,676

Compress everything your AI agent reads — tool outputs, logs, RAG chunks, and files — before it reaches the LLM, achieving 60–95% fewer tokens with the same answers.

View details
85
Repo Health
86
Technical
69
Dependency
Built with
Python 82%
Rust 12%
Updated 6 days ago
TypeScript
91%
Apache 2.0

Helicone

AI Development · Analytics · Monitoring

6,235

An open-source AI gateway and LLM observability platform that routes requests to 100+ models while logging cost, latency, and full traces for every call.

View details
74
Repo Health
81
Technical
65
Dependency
Built with
TypeScript 91%
Updated 6 days ago
TypeScript
71%
Other

highlight.io

Analytics · Developer Tools · Monitoring

9,384

Open-source full-stack monitoring that unifies session replay, error tracking, logging, and distributed tracing so you can stop context-switching between tools.

View details
76
Repo Health
78
Technical
65
Dependency
Built with
TypeScript 71%
Go 16%
Updated 1 weeks ago
TypeScript
55%
Other

Jaaz

AI Agents · AI Design Tools

6,721

Open-source AI creative agent that turns visual sketches and canvas gestures into images and videos — no text prompts required.

View details
45
Repo Health
59
Technical
71
Dependency
Built with
TypeScript 55%
Python 34%
JavaScript 10%
Updated 8 months ago
Python
64%
Other

Keep

Automation · Devops · Monitoring

12,433

The open-source AIOps and alert management platform that unifies 130+ monitoring tools into a single pane of glass with AI-powered correlation, deduplication, and workflow automation.

View details
88
Repo Health
79
Technical
66
Dependency
Built with
Python 64%
TypeScript 36%
Updated 1 weeks ago
Python
51%
AGPL 3.0

Khoj

AI Assistants · Knowledge Management · Productivity

37,802

A self-hostable AI second brain that chats with your documents, searches the web, builds custom agents, and runs entirely on your own LLM.

View details
62
Repo Health
82
Technical
66
Dependency
Built with
Python 51%
TypeScript 36%
Updated 2 months ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers