React
The official OpenAI Python SDK that provides a typed, async-capable HTTP client for interacting with all OpenAI API services including chat completions, audio transcription/speech, image generation, file uploads, assistants, realtime WebSocket APIs, and video generation with Sora. It supports both OpenAI and Azure OpenAI endpoints with comprehensive streaming, retry logic, and Pydantic-based request/response validation.
Tech Stack
Key Features
- Sync/Async HTTP Client
- Chat Completions with Streaming
- Realtime WebSocket API
- Audio Processing
- Azure OpenAI Support
- Structured Output Parsing
- Assistants & Threads API
- CLI Tool
Entry Points
Start here when working with this codebase
src/openai/__init__.pyPublic API exposure, module-level client management, exports all public classes and functions
Start here when adding new public exports, understanding what's exposed to users, or modifying package-level configuration
src/openai/_client.pyMain OpenAI and AsyncOpenAI client classes with resource accessors and configuration
Start here when adding new API resource accessors, modifying client initialization, or changing authentication handling
src/openai/_base_client.pyCore HTTP client base class with request execution, retries, response processing, and error handling
Start here when modifying HTTP behavior, retry logic, streaming infrastructure, or error handling patterns
src/openai/__main__.pyExecute CLI when package is invoked as module (python -m openai)
Start here when understanding CLI invocation or adding module-level CLI behavior
tests/conftest.pyCentralized pytest fixtures and test configuration
Start here when adding new test fixtures, understanding test setup, or modifying test infrastructure
How to Make Changes
Common modification patterns for this codebase
Add a new API resource endpoint
src/openai/types/Create type definitions for request params and response models. Create a new file like src/openai/types/your_resource.py
Pattern: Follow src/openai/types/completion.py for response types and src/openai/types/completion_create_params.py for request params using TypedDict
src/openai/types/__init__.pyExport your new types from the types module
Pattern: Follow existing exports pattern: from .your_resource import YourResourceResponse
src/openai/resources/Create resource class file like src/openai/resources/your_resource.py with sync and async classes
Pattern: Follow src/openai/resources/completions.py - create YourResource(SyncAPIResource) and AsyncYourResource(AsyncAPIResource) classes with create/list/retrieve methods
src/openai/resources/__init__.pyExport your resource classes and add to __all__
Pattern: Follow existing pattern: from .your_resource import YourResource, AsyncYourResource
src/openai/_client.pyAdd resource accessor property to OpenAI and AsyncOpenAI classes
Pattern: Follow existing pattern like @cached_property def completions(self) -> Completions: return Completions(self)
tests/api_resources/Create test file tests/api_resources/test_your_resource.py
Pattern: Follow tests/api_resources/test_completions.py - use parametrize for sync/async, mock responses with respx
Conventions to follow:
Add a new beta API resource
src/openai/types/beta/Create type definitions in src/openai/types/beta/your_beta_resource.py
Pattern: Follow src/openai/types/beta/assistant.py for response models and src/openai/types/beta/assistant_create_params.py for request params
src/openai/types/beta/__init__.pyExport your beta types
Pattern: Follow existing exports in the file
src/openai/resources/beta/Create resource class in src/openai/resources/beta/your_beta_resource.py
Pattern: Follow src/openai/resources/beta/assistants.py - include polling methods if needed using _poll_until_done pattern
src/openai/resources/beta/__init__.pyExport your beta resource classes
Pattern: Follow existing pattern in the file
src/openai/_client.pyEnsure beta accessor exists (it should), your resource will be accessed via client.beta.your_resource
Pattern: Beta resources are nested under the Beta resource class
Conventions to follow:
Add a new library helper utility
src/openai/lib/Create helper module like src/openai/lib/your_helper.py
Pattern: Follow src/openai/lib/_parsing.py for tool parsing helpers or src/openai/lib/streaming/_assistants.py for streaming utilities
src/openai/lib/__init__.pyExport your helper if it should be public
Pattern: Follow existing exports - only export user-facing utilities
src/openai/__init__.pyIf helper should be top-level accessible, add export here
Pattern: Follow how parse_chat_completion is exported
tests/lib/Create test file tests/lib/test_your_helper.py
Pattern: Follow tests/lib/test_azure.py or tests/lib/test_parsing.py
Conventions to follow:
Add a new CLI command
src/openai/cli/Add command implementation in appropriate CLI module under src/openai/cli/
Pattern: Examine existing CLI structure in src/openai/cli/ directory
src/openai/cli/__init__.pyRegister your command with the CLI parser
Pattern: Follow existing command registration patterns
tests/Add CLI tests if applicable
Pattern: Test CLI commands by invoking them programmatically
Conventions to follow:
Add a new example
examples/Create example file like examples/your_example.py
Pattern: Follow examples/demo.py for basic patterns or examples/realtime/realtime.py for async patterns
examples/your_example.pyInclude proper imports, client initialization, and clear comments
Pattern: Follow examples/audio.py for multi-function examples or examples/picture.py for single-purpose examples
Conventions to follow:
Add internal utility functions
src/openai/_utils/Add utility to appropriate module in src/openai/_utils/ or create new module
Pattern: Follow src/openai/_utils/_transform.py for data transformation utilities or src/openai/_utils/_streams.py for streaming utilities
src/openai/_utils/__init__.pyExport your utility if it needs to be used across modules
Pattern: Follow existing exports in the file
tests/Add tests for your utility
Pattern: Follow tests/test_utils.py patterns
Conventions to follow:
Add support for optional dependency
src/openai/_extras/Create proxy module like src/openai/_extras/your_dep_proxy.py
Pattern: Follow src/openai/_extras/numpy_proxy.py or src/openai/_extras/pandas_proxy.py - use lazy import with helpful error message
src/openai/_extras/__init__.pyExport your proxy
Pattern: Follow existing exports pattern
Conventions to follow:
Modify HTTP client behavior
src/openai/_base_client.pyModify base client methods for request/response handling
Pattern: Study existing methods like _request(), _retry_request(), _process_response()
src/openai/_client.pyIf changes affect client initialization or configuration, modify here
Pattern: Follow existing __init__ patterns for OpenAI and AsyncOpenAI classes
tests/test_client.pyAdd tests for your HTTP behavior changes
Pattern: Follow existing tests in tests/test_client.py - use respx for mocking HTTP
Conventions to follow:
Coding Conventions
Standards and patterns used in this codebase
File naming uses: kebab-case
azure_ad.py, responses_input_tokens.py, detect-breaking-changes.py, conftest.py, test_client.pyShared utilities are in utils/ directory
Utility files: scripts/utils/ruffen-docs.py, src/openai/helpers/local_audio_player.pyStandard project structure
Code is organized by feature/module