@@ -73,3 +73,56 @@ model = OpenRouterModel('openai/gpt-5')
7373agent = Agent(model, model_settings = settings)
7474...
7575```
76+
77+ ## Image Generation
78+
79+ You can use OpenRouter models that support image generation with the ` openrouter_modalities ` setting:
80+
81+ ``` python
82+ from pydantic_ai import Agent, BinaryImage
83+ from pydantic_ai.models.openrouter import OpenRouterModel, OpenRouterModelSettings
84+
85+ settings = OpenRouterModelSettings(
86+ openrouter_modalities = [' image' , ' text' ]
87+ )
88+ model = OpenRouterModel(' google/gemini-2.5-flash-image-preview' , provider = provider)
89+ agent = Agent(model = model, output_type = str | BinaryImage, model_settings = settings)
90+
91+ result = await agent.run(' A cat' )
92+ # result.output is a BinaryImage
93+ ```
94+
95+ You can further customize image generation using ` openrouter_image_config ` :
96+
97+ ``` python
98+ from pydantic_ai.models.openrouter import OpenRouterModelSettings
99+
100+ settings = OpenRouterModelSettings(
101+ openrouter_modalities = [' image' , ' text' ],
102+ openrouter_image_config = {' aspect_ratio' : ' 3:2' }
103+ )
104+ ```
105+
106+ > Available aspect ratios: ` '1:1' ` , ` '2:3' ` , ` '3:2' ` , ` '3:4' ` , ` '4:3' ` , ` '4:5' ` , ` '5:4' ` , ` '9:16' ` , ` '16:9' ` , ` '21:9' ` .
107+
108+ Image generation also works with streaming:
109+
110+ ``` python
111+ from pydantic_ai import Agent, BinaryImage
112+ from pydantic_ai.models.openrouter import OpenRouterModel, OpenRouterModelSettings
113+
114+ settings = OpenRouterModelSettings(
115+ openrouter_modalities = [' image' , ' text' ],
116+ openrouter_image_config = {' aspect_ratio' : ' 3:2' }
117+ )
118+ model = OpenRouterModel(' google/gemini-2.5-flash-image-preview' , provider = provider)
119+ agent = Agent(model = model, output_type = str | BinaryImage, model_settings = settings)
120+
121+ async with agent.run_stream(' A dog' ) as result:
122+ async for output in result.stream_output():
123+ if isinstance (output, str ):
124+ print (output)
125+ elif isinstance (output, BinaryImage):
126+ # Handle the generated image
127+ print (f ' Generated image: { output.media_type} ' )
128+ ```
0 commit comments