Implementation:Openai Openai python Module Level Client
| Knowledge Sources | |
|---|---|
| Domains | SDK_Infrastructure |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for module-level lazy API access provided by the openai-python SDK.
Description
The _module_client module defines 21 proxy classes that enable module-level attribute access for all API resource namespaces (e.g., openai.chat, openai.files, openai.completions). Each proxy class extends LazyProxy[T] and overrides the __load__() method to call _load_client() and return the corresponding resource attribute from the default OpenAI client singleton. The proxy classes include ChatProxy, BetaProxy, FilesProxy, AudioProxy, EvalsProxy, ImagesProxy, ModelsProxy, SkillsProxy, VideosProxy, BatchesProxy, UploadsProxy, WebhooksProxy, RealtimeProxy, ResponsesProxy, EmbeddingsProxy, ContainersProxy, CompletionsProxy, ModerationsProxy, FineTuningProxy, VectorStoresProxy, and ConversationsProxy. Module-level instances are created using __as_proxied__() to provide correctly typed attributes.
Usage
Use this module implicitly when accessing API resources at the module level, such as openai.chat.completions.create(...), without first constructing an explicit client instance.
Code Reference
Source Location
- Repository: openai-python
- File: src/openai/_module_client.py
- Lines: 1-181
Signature
class ChatProxy(LazyProxy["Chat"]):
def __load__(self) -> Chat: ...
class BetaProxy(LazyProxy["Beta"]):
def __load__(self) -> Beta: ...
class FilesProxy(LazyProxy["Files"]):
def __load__(self) -> Files: ...
# ... 18 more proxy classes following the same pattern ...
# Module-level instances
chat: Chat
beta: Beta
files: Files
audio: Audio
evals: Evals
images: Images
models: Models
skills: Skills
videos: Videos
batches: Batches
uploads: Uploads
webhooks: Webhooks
realtime: Realtime
responses: Responses
embeddings: Embeddings
containers: Containers
completions: Completions
moderations: Moderations
fine_tuning: FineTuning
vector_stores: VectorStores
conversations: Conversations
Import
import openai
# Access resources directly at module level
openai.chat
openai.files
openai.completions
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (none) | N/A | N/A | Proxy classes take no explicit input; they lazily load the default client |
Outputs
| Name | Type | Description |
|---|---|---|
| __load__() result | Resource class (e.g., Chat, Files) | The fully initialized resource object from the default OpenAI client |
Usage Examples
Basic Usage
import openai
# Module-level access triggers lazy client initialization
response = openai.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello"}],
)
# Equivalent to:
# client = openai.OpenAI()
# response = client.chat.completions.create(...)