Skip to content

Add image generation support to OpenRouterProvider#333

Merged
pushpak1300 merged 3 commits intolaravel:0.xfrom
billyfranklim1:feat/openrouter-image-generation
May 1, 2026
Merged

Add image generation support to OpenRouterProvider#333
pushpak1300 merged 3 commits intolaravel:0.xfrom
billyfranklim1:feat/openrouter-image-generation

Conversation

@billyfranklim1
Copy link
Copy Markdown
Contributor

Summary

This PR adds ImageProvider support to OpenRouterProvider, allowing image generation through OpenRouter's unified API.

OpenRouter supports image generation via the /api/v1/chat/completions endpoint using the modalities parameter (docs), but OpenRouterProvider only implemented EmbeddingProvider and TextProvider, causing a LogicException when attempting Image::of()->generate(provider: Lab::OpenRouter).

Changes

  • OpenRouterImageGateway — New gateway that sends image generation requests to OpenRouter's chat completions endpoint with modalities: ["image", "text"], parses base64 data URI image responses, and maps them to GeneratedImage instances.
  • OpenRouterProvider — Now implements ImageProvider using the GeneratesImages and HasImageGateway traits. Adds defaultImageModel() (google/gemini-2.5-flash-image), defaultImageOptions() (maps size to image_config.aspect_ratio), and imageGateway().
  • Tests — 4 unit tests covering: basic generation, aspect ratio config passthrough, empty response handling, and multiple images in response.

Usage

use Laravel\Ai\Image;
use Laravel\Ai\Enums\Lab;

$response = Image::of('A sunset over mountains')
    ->landscape()
    ->generate(provider: Lab::OpenRouter, model: 'google/gemini-2.5-flash-image');

$image = $response->firstImage();
$binary = $image->content();

Fixes #330

@billyfranklim1 billyfranklim1 force-pushed the feat/openrouter-image-generation branch from 08c9a96 to 39f4b8a Compare March 30, 2026 20:46
@pushpak1300
Copy link
Copy Markdown
Member

Can you fix merge conflicts and refactor to new namespace we have. i'm marking this draft once ready please mark this for review.

@pushpak1300 pushpak1300 marked this pull request as draft April 23, 2026 13:48
billyfranklim1 and others added 2 commits April 30, 2026 18:56
OpenRouter's API supports image generation via chat/completions with
the modalities parameter, but OpenRouterProvider only implemented
EmbeddingProvider and TextProvider.

This adds:
- OpenRouterImageGateway: calls /api/v1/chat/completions with
  modalities: ["image", "text"] and parses base64 data URI responses
- ImageProvider implementation on OpenRouterProvider with support for
  aspect_ratio via image_config
- Tests for the gateway covering generation, aspect ratio, attachments,
  multiple images, and empty response handling

Fixes laravel#330
@pushpak1300 pushpak1300 force-pushed the feat/openrouter-image-generation branch from 39f4b8a to ad23bdd Compare April 30, 2026 13:46
@pushpak1300 pushpak1300 marked this pull request as ready for review April 30, 2026 13:46
@pushpak1300 pushpak1300 merged commit 53c778e into laravel:0.x May 1, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenRouterProvider does not implement ImageProvider (image generation support)

2 participants