Skip to main content

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.

OpenRouterImageModel sends generation requests to the /images endpoint and parses the base64-encoded image data returned by the API. It uses the shared ImageRequestBuilder and ImageResponseParser from the OpenAI-compatible adapter, with a few OpenRouter-specific adjustments handled automatically.

Basic usage

Pass an image model identifier to OpenRouter::image() and chain it with the Generate::image() builder. Call ->run() to execute the request and receive an ImageResponse.
use AiSdk\Generate;
use AiSdk\OpenRouter;

$result = Generate::image()
    ->model(OpenRouter::image('openai/gpt-image-1'))
    ->prompt('A clean app icon for a PHP AI SDK')
    ->size('1024x1024')
    ->run();

$result->output->save(__DIR__.'/icon.png');

Saving the output

Call save() on the output object with a local file path to write the image to disk:
$result->output->save(__DIR__.'/image.png');

Response fields

Generate::image()->run() returns an ImageResponse. The output is available under $result->output:
FieldTypeDescription
$result->output->base64stringRaw base64-encoded image data returned by the API.
$result->output->mimeTypestringMIME type of the image (e.g. image/png, image/svg+xml).
$result->usage->totalTokensintTotal tokens consumed by the request.

Supported parameters

The Generate::image() builder supports the following methods when targeting OpenRouter:
MethodDescription
prompt(string)The text description of the image to generate.
size(string)Image dimensions, e.g. '1024x1024'.
count(int)Number of images to generate (n in the request body).
aspectRatio(string)Aspect ratio sent as the aspect_ratio parameter (e.g. '1:1', '16:9').
providerOptions(string, array)Arbitrary provider-level options merged into the request body.
OpenRouter does not accept a response_format field in image requests. The SDK omits it automatically — you should never pass response_format manually when using this provider. Aspect ratio is sent as the aspect_ratio parameter (not aspectRatio) to match the OpenRouter API contract.

Recraft vector example

The test suite confirms the full request flow for Recraft models. The aspect_ratio parameter and provider-level style options are both forwarded correctly:
use AiSdk\Generate;
use AiSdk\OpenRouter;

$result = Generate::image()
    ->model(OpenRouter::image('recraft/recraft-v4.1-vector'))
    ->prompt('A vector bird')
    ->count(1)
    ->aspectRatio('1:1')
    ->providerOptions('openrouter', [
        'raw' => [
            'provider' => [
                'options' => [
                    'recraft' => ['style' => 'vector'],
                ],
            ],
        ],
    ])
    ->run();

echo $result->output->mimeType;   // image/svg+xml
echo $result->usage->totalTokens; // integer
$result->output->save(__DIR__.'/bird.svg');
The SDK sends aspect_ratio as a top-level request parameter and merges the provider block from raw directly into the request body. The response_format key is never included.

Build docs developers (and LLMs) love