Image Generation

How to generate images with OpenRouter models

OpenRouter supports image generation through models that have "image" in their output_modalities. These models can create images from text prompts when you specify the appropriate modalities in your request.

Model Discovery

You can find image generation models in several ways:

On the Models Page

Visit the Models page and filter by output modalities to find models capable of image generation. Look for models that list "image" in their output modalities.

In the Chatroom

When using the Chatroom, click the Image button to automatically filter and select models with image generation capabilities. If no image-capable model is active, you’ll be prompted to add one.

API Usage

To generate images, send a request to the /api/v1/chat/completions endpoint with the modalities parameter set to include both "image" and "text".

Basic Image Generation

1import requests
2import json
3
4url = "https://openrouter.ai/api/v1/chat/completions"
5headers = {
6 "Authorization": f"Bearer {API_KEY_REF}",
7 "Content-Type": "application/json"
8}
9
10payload = {
11 "model": "{{MODEL}}",
12 "messages": [
13 {
14 "role": "user",
15 "content": "Generate a beautiful sunset over mountains"
16 }
17 ],
18 "modalities": ["image", "text"]
19}
20
21response = requests.post(url, headers=headers, json=payload)
22result = response.json()
23
24# The generated image will be in the assistant message
25if result.get("choices"):
26 message = result["choices"][0]["message"]
27 if message.get("images"):
28 for image in message["images"]:
29 image_url = image["image_url"]["url"] # Base64 data URL
30 print(f"Generated image: {image_url[:50]}...")

Streaming Image Generation

Image generation also works with streaming responses:

1import requests
2import json
3
4url = "https://openrouter.ai/api/v1/chat/completions"
5headers = {
6 "Authorization": f"Bearer {API_KEY_REF}",
7 "Content-Type": "application/json"
8}
9
10payload = {
11 "model": "{{MODEL}}",
12 "messages": [
13 {
14 "role": "user",
15 "content": "Create an image of a futuristic city"
16 }
17 ],
18 "modalities": ["image", "text"],
19 "stream": True
20}
21
22response = requests.post(url, headers=headers, json=payload, stream=True)
23
24for line in response.iter_lines():
25 if line:
26 line = line.decode('utf-8')
27 if line.startswith('data: '):
28 data = line[6:]
29 if data != '[DONE]':
30 try:
31 chunk = json.loads(data)
32 if chunk.get("choices"):
33 delta = chunk["choices"][0].get("delta", {})
34 if delta.get("images"):
35 for image in delta["images"]:
36 print(f"Generated image: {image['image_url']['url'][:50]}...")
37 except json.JSONDecodeError:
38 continue

Response Format

When generating images, the assistant message includes an images field containing the generated images:

1{
2 "choices": [
3 {
4 "message": {
5 "role": "assistant",
6 "content": "I've generated a beautiful sunset image for you.",
7 "images": [
8 {
9 "type": "image_url",
10 "image_url": {
11 "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
12 }
13 }
14 ]
15 }
16 }
17 ]
18}

Image Format

  • Format: Images are returned as base64-encoded data URLs
  • Types: Typically PNG format (data:image/png;base64,)
  • Multiple Images: Some models can generate multiple images in a single response
  • Size: Image dimensions vary by model capabilities

Model Compatibility

Not all models support image generation. To use this feature:

  1. Check Output Modalities: Ensure the model has "image" in its output_modalities
  2. Set Modalities Parameter: Include "modalities": ["image", "text"] in your request
  3. Use Compatible Models: Examples include:
    • google/gemini-2.5-flash-image-preview
    • Other models with image generation capabilities

Best Practices

  • Clear Prompts: Provide detailed descriptions for better image quality
  • Model Selection: Choose models specifically designed for image generation
  • Error Handling: Check for the images field in responses before processing
  • Rate Limits: Image generation may have different rate limits than text generation
  • Storage: Consider how you’ll handle and store the base64 image data

Troubleshooting

No images in response?

  • Verify the model supports image generation (output_modalities includes "image")
  • Ensure you’ve included "modalities": ["image", "text"] in your request
  • Check that your prompt is requesting image generation

Model not found?

  • Use the Models page to find available image generation models
  • Filter by output modalities to see compatible models