Implementation:Openai Openai python SDK Package Exports
| Knowledge Sources | |
|---|---|
| Domains | SDK_Infrastructure, Python |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for package initialization and module-level client configuration provided by the openai-python SDK.
Description
The __init__.py module serves as the main entry point for the openai package. It performs three key roles:
- Public API re-exports: Imports and re-exports all user-facing classes, exceptions, types, and utilities from internal submodules. The
__all__list (lines 40-85) defines the complete public surface area includingOpenAI,AsyncOpenAI,Stream,AsyncStream,BaseModel, all exception types, and default configuration constants.
- Module-level client (
_ModuleClient): Defines a_ModuleClientclass (line 155) that subclassesOpenAIand overrides key properties (api_key,organization,project,webhook_secret,base_url,timeout,max_retries) as Python properties that read from and write to module-level global variables. This enables the convenience patternopenai.api_key = "sk-...". A companion_AzureModuleClientclass (line 271) extends both_ModuleClientandAzureOpenAIfor Azure scenarios.
- Lazy client loading (
_load_client()): The_load_client()function (line 301) lazily instantiates the singleton module-level client. It detects whether to create an OpenAI or Azure client based on environment variables (OPENAI_API_KEY,AZURE_OPENAI_ENDPOINT,AZURE_OPENAI_AD_TOKEN,OPENAI_API_TYPE) and raises_AmbiguousModuleClientUsageErrorif both credential sets are present without an explicitapi_type.
The module also patches __module__ attributes on all exported symbols (lines 105-112) so that error messages show openai.NotFoundError instead of openai._exceptions.NotFoundError.
Usage
Use this module whenever you import openai or use the module-level convenience API (e.g., openai.api_key = "..."). For explicit client instantiation, prefer OpenAI(...) or AsyncOpenAI(...) directly.
Code Reference
Source Location
- Repository: openai-python
- File: src/openai/__init__.py
- Lines: 1-397
Signature
class _ModuleClient(OpenAI):
@property
@override
def api_key(self) -> str | None: ...
@property
@override
def organization(self) -> str | None: ...
@property
@override
def project(self) -> str | None: ...
@property
@override
def base_url(self) -> httpx.URL: ...
@property
@override
def timeout(self) -> float | Timeout | None: ...
@property
@override
def max_retries(self) -> int: ...
def _load_client() -> OpenAI: ...
def _reset_client() -> None: ...
Import
import openai
from openai import OpenAI, AsyncOpenAI, APIError, BaseModel
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| api_key | str or None | No | Module-level OpenAI API key, read from global or OPENAI_API_KEY env var
|
| organization | str or None | No | Module-level organization ID |
| project | str or None | No | Module-level project ID |
| base_url | str or httpx.URL or None | No | Custom API base URL override |
| timeout | float or Timeout or None | No | Request timeout; defaults to DEFAULT_TIMEOUT
|
| max_retries | int | No | Maximum number of retries; defaults to DEFAULT_MAX_RETRIES
|
| api_type | Literal["openai", "azure"] or None | No | Explicit API backend type; auto-detected from credentials if not set |
| http_client | httpx.Client or None | No | Custom httpx client to use for requests |
Outputs
| Name | Type | Description |
|---|---|---|
| _client | OpenAI | Singleton module-level client instance (either _ModuleClient or _AzureModuleClient)
|
Usage Examples
Basic Usage
import openai
# Module-level convenience pattern
openai.api_key = "sk-..."
completion = openai.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello"}]
)
Explicit Client Instantiation
from openai import OpenAI
client = OpenAI(api_key="sk-...")
completion = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello"}]
)
Azure Module-Level Client
import openai
openai.api_type = "azure"
openai.azure_endpoint = "https://my-resource.openai.azure.com"
openai.api_key = "azure-key-..."
openai.api_version = "2024-02-01"