TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/openai/llms.txt
Use this file to discover all available pages before exploring further.
OpenAI class is a static facade that manages a singleton OpenAIProvider instance and exposes factory methods for text and image models. Rather than constructing a provider directly, most application code interacts exclusively with OpenAI::model() and OpenAI::image(), while OpenAI::create() handles one-time provider configuration. The underlying OpenAIProvider and OpenAIOptions classes are available when you need direct access — for example, when wiring the provider into a dependency injection container or sharing options between multiple provider instances.
OpenAI Static Methods
The OpenAI facade (namespace AiSdk) holds a private static ?OpenAIProvider $default property and delegates all model construction to it. The RegistersModels trait mixed in by the class adds registerModel() support.
OpenAI::create()
OpenAIProvider from the supplied config array, stores it as the singleton, and returns it. Every subsequent call to OpenAI::model() or OpenAI::image() will use this provider until OpenAI::reset() is called.
The $config array is forwarded directly to OpenAIOptions::fromArray(). All keys are optional — missing keys fall back to environment variables or built-in defaults.
OpenAI API key. Falls back to the
OPENAI_API_KEY environment variable. Required — an exception is thrown if neither the argument nor the environment variable is set.Base URL for all API requests. Falls back to the
OPENAI_BASE_URL environment variable. Trailing slashes are stripped automatically.OpenAI organization ID. When set, it is sent as the
OpenAI-Organization header on every request. Falls back to the OPENAI_ORGANIZATION environment variable.Additional HTTP headers merged into every request. Keys and values must be strings. These are merged with the
Authorization header produced from apiKey, with your custom headers taking precedence.A pre-configured
AiSdk\Support\Sdk instance that wraps a PSR-18 HTTP client, PSR-17 request factory, and PSR-17 stream factory. Provide this when you need to supply a custom HTTP stack — for example, during testing with a fake HTTP client.The newly created provider, which is also stored as the facade singleton.
OpenAI::model()
The OpenAI model identifier, e.g.
'gpt-4o', 'o3', 'gpt-4.1-mini'. Wildcard catalog patterns such as gpt-4o* are resolved at capability-check time, not here.A
TextModelInterface instance (concretely OpenAITextModel) bound to the given model ID and the current provider options.OpenAI::image()
OpenAIProvider::imageModel() internally, which constructs an OpenAIImageModel.
The OpenAI image model identifier, e.g.
'gpt-image-1', 'dall-e-3', 'dall-e-2'.An
ImageModelInterface instance (concretely OpenAIImageModel) bound to the given model ID.OpenAI::default()
OpenAIProvider. If no provider has been registered via OpenAI::create(), one is created on demand by calling self::create() with an empty config array, which resolves all settings from environment variables.
The active singleton provider.
OpenAI::reset()
null. The next call to any facade method will trigger lazy re-creation. This method is primarily useful in test suites where each test case needs a clean provider state.
OpenAI::registerModel()
models.json catalog lookup.
Either a plain model ID string (used together with
$capabilities) or a fully constructed ModelDefinition object that already encapsulates the capability list.An array of
Capability enum values. Only used when $model is a string. Ignored when a ModelDefinition object is passed.ModelDefinition form:
OpenAIProvider Class
OpenAIProvider (namespace AiSdk\OpenAI) is the concrete provider returned by OpenAI::create(). It extends BaseProvider and holds an immutable OpenAIOptions instance. You can construct it directly when you want to manage provider lifetime yourself rather than relying on the static facade.
Constructor
A fully constructed
OpenAIOptions value object. Use OpenAIOptions::fromArray($config) to build one from a config array, or construct it directly with named arguments.Instance Methods
textModel(string $modelId): TextModelInterface
Creates and returns a new OpenAITextModel instance bound to this provider’s options and model registry.
imageModel(string $modelId): ImageModelInterface
Creates and returns a new OpenAIImageModel instance bound to this provider’s options and model registry.
name(): string
Returns the string 'openai' — the canonical provider identifier used in capability source annotations and exception messages.
OpenAIOptions Value Object
OpenAIOptions (namespace AiSdk\OpenAI) is an immutable value object that carries all connection and authentication settings for the provider. It is constructed either directly or via the fromArray() static factory, which also handles environment variable resolution.
Properties
| Property | Type | Default | Source |
|---|---|---|---|
apiKey | string | — | OPENAI_API_KEY env var or config['apiKey'] |
baseUrl | string | https://api.openai.com/v1 | OPENAI_BASE_URL env var or config['baseUrl'] |
organization | string|null | null | OPENAI_ORGANIZATION env var or config['organization'] |
headers | array | [] | config['headers'] (constructor only) |
sdk | Sdk|null | null | config['sdk'] (constructor only) |
readonly. To change settings, create a new OpenAIOptions instance.
fromArray(array $config = []): self
Static factory that resolves each option from the config array, falling back to environment variables where applicable. Trailing slashes are stripped from baseUrl. Throws if apiKey cannot be resolved from either source.
authHeaders(): array
Returns the final merged HTTP headers array that is sent with every API request. The method always includes Authorization: Bearer {apiKey} and merges in the custom headers array. If organization is non-null, OpenAI-Organization: {organization} is appended.
Custom headers passed via
config['headers'] are merged after the Authorization header using array_merge, so they take precedence over all default headers — including Authorization. Do not include an Authorization key in headers unless you intend to replace the default bearer token.