Skip to main content
The main client class for interacting with the OpenAI API synchronously.

Constructor

Parameters

str | Callable[[], str] | None
default:"None"
Your OpenAI API key. If not provided, the client will attempt to read it from the OPENAI_API_KEY environment variable. Required.Can also be a callable that returns a string, which will be called to refresh the API key when needed.
str | None
default:"None"
Your OpenAI organization ID. If not provided, the client will attempt to read it from the OPENAI_ORG_ID environment variable.When provided, this will be sent as the OpenAI-Organization header on all requests.
str | None
default:"None"
Your OpenAI project ID. If not provided, the client will attempt to read it from the OPENAI_PROJECT_ID environment variable.When provided, this will be sent as the OpenAI-Project header on all requests.
str | None
default:"None"
Secret key for validating webhook signatures. If not provided, the client will attempt to read it from the OPENAI_WEBHOOK_SECRET environment variable.
str | httpx.URL | None
default:"None"
Override the default base URL for the API. If not provided, the client will attempt to read it from the OPENAI_BASE_URL environment variable, otherwise defaults to https://api.openai.com/v1.
str | httpx.URL | None
default:"None"
Base URL for WebSocket connections (used for Realtime API).If not specified, the default base URL will be used with the scheme changed to wss://. For example, https://api.openai.com becomes wss://api.openai.com.
float | Timeout | None
default:"600 seconds"
Request timeout configuration. Can be:
  • A float representing timeout in seconds
  • An httpx.Timeout object for fine-grained control
  • None to disable timeouts
Default is 600 seconds (10 minutes) for the overall timeout and 5 seconds for connect timeout.
int
default:"2"
Maximum number of retries for failed requests. Set to 0 to disable retries.The client will automatically retry requests that fail due to network errors or certain HTTP status codes (429, 500, 502, 503, 504).
Mapping[str, str] | None
default:"None"
Additional HTTP headers to include on all requests.
Mapping[str, object] | None
default:"None"
Default query parameters to include on all requests.
httpx.Client | None
default:"None"
Custom httpx.Client instance to use for making requests.You can use DefaultHttpxClient to retain the default values for limits, timeout, and follow_redirects while customizing other options.

Properties

The client provides access to various API resources through properties:
  • client.chat - Chat completions
  • client.completions - Text completions
  • client.embeddings - Embeddings
  • client.files - File operations
  • client.images - Image generation
  • client.audio - Audio transcription and generation
  • client.moderations - Content moderation
  • client.models - Model information
  • client.fine_tuning - Fine-tuning operations
  • client.batches - Batch API
  • client.uploads - Multipart file uploads
  • client.beta - Beta features (Assistants, Threads, etc.)
  • client.vector_stores - Vector store operations
  • client.realtime - Realtime API
  • client.webhooks - Webhook utilities

Examples

Basic usage

With explicit API key

With organization and project

Custom timeout configuration

Custom retry configuration

Custom HTTP client with connection pooling

Using a callable for dynamic API key

Custom headers

Using with_options for per-request configuration

Methods

copy()

Create a new client instance with the same configuration, optionally overriding specific options.

with_options()

Alias for copy(), useful for inline usage: