Documentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/openrouter/llms.txt
Use this file to discover all available pages before exploring further.
OpenRouterTextModel implements the PHP AI SDK’s TextModelInterface. It builds chat completion requests using the shared ChatRequestBuilder from aisdk/openai-compatible, sends them to the /chat/completions endpoint, and parses the response via ChatResponseParser (for standard completions) or ChatStreamParser (for streaming). This keeps the OpenRouter integration thin and consistent with all other OpenAI-compatible providers in the SDK ecosystem.
Namespace: AiSdk\OpenRouter\ModelsFile:
src/Models/OpenRouterTextModel.php
Constructor
The OpenRouter model identifier passed at construction, e.g.
'openai/gpt-4o' or 'anthropic/claude-sonnet-4'. Returned verbatim by modelId().The provider configuration, including API key, base URL, and extra headers. See
OpenRouterOptions.An optional model registry for overriding capability definitions at runtime. When
null, capability lookups fall through to the bundled resources/models.json catalog.OpenRouterTextModel instances are not normally created directly. Use OpenRouter::model() or OpenRouterProvider::textModel() instead.Methods
provider()
string — always 'openrouter'.
modelId()
string — the model ID, e.g. 'openai/gpt-4o'.
capabilities()
- The injected
ModelRegistry, if one was provided. - The bundled
resources/models.jsoncatalog.
array<int, Capability> — an array of Capability enum cases supported by the model.
capability()
- Any capability override configured directly on the model instance.
- The injected
ModelRegistry. - The bundled
resources/models.jsoncatalog.
Capability::TextGeneration and the model is not found in the catalog at all, the method returns CapabilitySupport::supported(...) with reason 'unknown-model-fallback'. This ensures that arbitrary OpenRouter-routed models are assumed to support text generation unless explicitly marked otherwise.
The
Capability enum case to check, e.g. Capability::TextGeneration, Capability::Reasoning, or Capability::ImageInput.CapabilitySupport — the support level for the requested capability.
generate()
ChatRequestBuilder::build()serialises$requestinto an OpenAI-compatible JSON body withstream: false.- The body is POSTed to
Url::joinPath($options->baseUrl, '/chat/completions')with the headers from$options->authHeaders(). ChatResponseParser::parse()maps the JSON payload to aTextModelResponse.
The text generation request, including messages, system prompt, temperature, and any other generation parameters.
TextModelResponse — contains text, usage (input/output token counts), finishReason, and providerMetadata['openrouter'] with raw fields such as id and model.
stream()
ChatRequestBuilder::build()serialises$requestwithstream: true.- The body is POSTed to
Url::joinPath($options->baseUrl, '/chat/completions')viapostStream(). ChatStreamParser::parse()reads the server-sent events and yields delta objects.
The text generation request. Identical in structure to the request accepted by
generate().Generator — yields parsed stream chunk objects containing delta text and finish information.
Endpoint
All requests are sent to:Usage Example
For a broader introduction to text generation and streaming, see the Text Generation and Streaming usage guides.