Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Openai Openai python SDK Package Exports

From Leeroopedia
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:

  1. 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 including OpenAI, AsyncOpenAI, Stream, AsyncStream, BaseModel, all exception types, and default configuration constants.
  1. Module-level client (_ModuleClient): Defines a _ModuleClient class (line 155) that subclasses OpenAI and 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 pattern openai.api_key = "sk-...". A companion _AzureModuleClient class (line 271) extends both _ModuleClient and AzureOpenAI for Azure scenarios.
  1. 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 _AmbiguousModuleClientUsageError if both credential sets are present without an explicit api_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

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"

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment