Overview
TheOpenAI::Client class is the primary entry point for interacting with the OpenAI API. It handles authentication, request configuration, and provides access to all API resources.
Basic Usage
Parameters
Your OpenAI API key. This parameter is required.Default:
ENV["OPENAI_API_KEY"]If not provided and the environment variable is not set, an
ArgumentError will be raised.Your OpenAI organization ID. Used for scoping API requests to a specific organization.Default:
ENV["OPENAI_ORG_ID"]This value is sent in the openai-organization header with each request.Your OpenAI project ID. Used for scoping API requests to a specific project.Default:
ENV["OPENAI_PROJECT_ID"]This value is sent in the openai-project header with each request.Your webhook secret for verifying webhook signatures.Default:
ENV["OPENAI_WEBHOOK_SECRET"]Override the default base URL for the API. Useful for testing or using alternative endpoints.Default:
ENV["OPENAI_BASE_URL"] or "https://api.openai.com/v1"Example:
"https://api.example.com/v2/"Maximum number of retries to attempt after a failed retryable request.Default:
2 (OpenAI::Client::DEFAULT_MAX_RETRIES)Per-request timeout in seconds.Default:
600.0 (OpenAI::Client::DEFAULT_TIMEOUT_IN_SECONDS)Initial retry delay in seconds. Overall delay is calculated using exponential backoff plus jitter.Default:
0.5 (OpenAI::Client::DEFAULT_INITIAL_RETRY_DELAY)Maximum retry delay in seconds.Default:
8.0 (OpenAI::Client::DEFAULT_MAX_RETRY_DELAY)Constants
TheOpenAI::Client class defines the following constants for default configuration values:
| Constant | Value | Description |
|---|---|---|
DEFAULT_MAX_RETRIES | 2 | Default max number of retries to attempt after a failed retryable request |
DEFAULT_TIMEOUT_IN_SECONDS | 600.0 | Default per-request timeout |
DEFAULT_INITIAL_RETRY_DELAY | 0.5 | Default initial retry delay in seconds (used with exponential backoff + jitter) |
DEFAULT_MAX_RETRY_DELAY | 8.0 | Default max retry delay in seconds |
Available Resources
Once initialized, the client provides access to the following resource namespaces:Error Handling
If the
api_key is not provided (either as a parameter or via the OPENAI_API_KEY environment variable), an ArgumentError will be raised: