Skip to content

Conversation

@khasinski
Copy link

OpenRouter uses the chat completions endpoint for image generation instead of a dedicated /images/generations endpoint. This commit adds an OpenRouter::Images module that:

  • Routes image generation requests to chat/completions
  • Formats the payload with modalities: ["image", "text"]
  • Parses the response from choices[0].message.images[]
  • Handles both base64 data URLs and regular URLs

What this does

When using RubyLLM.paint with OpenRouter, requests were being sent to https://openrouter.ai/api/v1/images/generations, which doesn't exist. OpenRouter returns a 405 Method Not Allowed error.

OpenRouter implements image generation via the chat completions endpoint (/chat/completions) with modalities: ["image", "text"], not through a dedicated images endpoint.

This PR adds an OpenRouter::Images module that overrides the inherited OpenAI::Images behavior to:

  1. Route to the correct endpoint: chat/completions instead of images/generations
  2. Format the request correctly: Uses chat message format with modalities: ["image", "text"]
  3. Parse the response correctly: Extracts images from choices[0].message.images[] as base64 data URLs

Tested with google/gemini-2.5-flash-image-preview via OpenRouter.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Performance improvement

Scope check

  • I read the Contributing Guide
  • This aligns with RubyLLM's focus on LLM communication
  • This isn't application-specific logic that belongs in user code
  • This benefits most users, not just my specific use case

Quality check

  • I ran overcommit --install and all hooks pass
  • I tested my changes thoroughly
    • For provider changes: Re-recorded VCR cassettes with bundle exec rake vcr:record[provider_name]
    • All tests pass: bundle exec rspec
  • I updated documentation if needed
  • I didn't modify auto-generated files manually (models.json, aliases.json)

API changes

  • Breaking change
  • New public methods/classes
  • Changed method signatures
  • No API changes

Related issues

Fixes #513

OpenRouter uses the chat completions endpoint for image generation
instead of a dedicated /images/generations endpoint. This commit adds
an OpenRouter::Images module that:

- Routes image generation requests to chat/completions
- Formats the payload with modalities: ["image", "text"]
- Parses the response from choices[0].message.images[]
- Handles both base64 data URLs and regular URLs
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.

[BUG] paint is not working with openrouter provider

1 participant