Skip to content

Commit 1185293

Browse files
committed
Add 'resolution' config to ImageGenerationTool
1 parent b2cbbea commit 1185293

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

docs/builtin-tools.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,23 @@ assert isinstance(result.output, BinaryImage)
353353

354354
_(This example is complete, it can be run "as is")_
355355

356+
To control the image resolution with Gemini 3 Pro Image models, use the `resolution` parameter:
357+
358+
```py {title="image_generation_google_resolution.py"}
359+
from pydantic_ai import Agent, BinaryImage, ImageGenerationTool
360+
361+
agent = Agent(
362+
'google-gla:gemini-3-pro-image-preview',
363+
builtin_tools=[ImageGenerationTool(aspect_ratio='16:9', resolution='4K')],
364+
output_type=BinaryImage,
365+
)
366+
367+
result = agent.run_sync('Generate a high-resolution wide landscape illustration of an axolotl.')
368+
assert isinstance(result.output, BinaryImage)
369+
```
370+
371+
_(This example is complete, it can be run "as is")_
372+
356373
For more details, check the [API documentation][pydantic_ai.builtin_tools.ImageGenerationTool].
357374

358375
#### Provider Support
@@ -368,6 +385,7 @@ For more details, check the [API documentation][pydantic_ai.builtin_tools.ImageG
368385
| `quality` |||
369386
| `size` |||
370387
| `aspect_ratio` | ✅ (1:1, 2:3, 3:2) ||
388+
| `resolution` || ✅ (1K, 2K, 4K) |
371389

372390
## Web Fetch Tool
373391

pydantic_ai_slim/pydantic_ai/builtin_tools.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,14 @@ class ImageGenerationTool(AbstractBuiltinTool):
328328
* OpenAI Responses (maps '1:1', '2:3', and '3:2' to supported sizes)
329329
"""
330330

331+
resolution: str | None = None
332+
"""The resolution/size tier for generated images.
333+
334+
Supported by:
335+
336+
* Google: '1K', '2K', '4K' for Gemini 3 Pro Image and compatible models
337+
"""
338+
331339
kind: str = 'image_generation'
332340
"""The kind of tool."""
333341

pydantic_ai_slim/pydantic_ai/models/google.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,14 @@ def _get_tools(
362362
raise UserError(
363363
"`ImageGenerationTool` is not supported by this model. Use a model with 'image' in the name instead."
364364
)
365-
if tool.aspect_ratio:
366-
image_config = ImageConfigDict(aspect_ratio=tool.aspect_ratio)
365+
# Build image_config from tool parameters, only including non-None values
366+
image_config_kwargs: dict[str, Any] = {}
367+
if tool.aspect_ratio is not None:
368+
image_config_kwargs['aspect_ratio'] = tool.aspect_ratio
369+
if tool.resolution is not None:
370+
image_config_kwargs['image_size'] = tool.resolution
371+
if image_config_kwargs:
372+
image_config = ImageConfigDict(**image_config_kwargs)
367373
else: # pragma: no cover
368374
raise UserError(
369375
f'`{tool.__class__.__name__}` is not supported by `GoogleModel`. If it should be, please file an issue.'

tests/models/test_google.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3652,6 +3652,26 @@ async def test_google_image_generation_tool_aspect_ratio(google_provider: Google
36523652
assert image_config == {'aspect_ratio': '16:9'}
36533653

36543654

3655+
async def test_google_image_generation_resolution(google_provider: GoogleProvider) -> None:
3656+
"""Test that resolution parameter from ImageGenerationTool is added to image_config."""
3657+
model = GoogleModel('gemini-3-pro-image-preview', provider=google_provider)
3658+
params = ModelRequestParameters(builtin_tools=[ImageGenerationTool(resolution='2K')])
3659+
3660+
tools, image_config = model._get_tools(params) # pyright: ignore[reportPrivateUsage]
3661+
assert tools is None
3662+
assert image_config == {'image_size': '2K'}
3663+
3664+
3665+
async def test_google_image_generation_resolution_with_aspect_ratio(google_provider: GoogleProvider) -> None:
3666+
"""Test that resolution and aspect_ratio from ImageGenerationTool work together."""
3667+
model = GoogleModel('gemini-3-pro-image-preview', provider=google_provider)
3668+
params = ModelRequestParameters(builtin_tools=[ImageGenerationTool(aspect_ratio='16:9', resolution='4K')])
3669+
3670+
tools, image_config = model._get_tools(params) # pyright: ignore[reportPrivateUsage]
3671+
assert tools is None
3672+
assert image_config == {'aspect_ratio': '16:9', 'image_size': '4K'}
3673+
3674+
36553675
async def test_google_vertexai_image_generation(allow_model_requests: None, vertex_provider: GoogleProvider):
36563676
model = GoogleModel('gemini-2.5-flash-image', provider=vertex_provider)
36573677

0 commit comments

Comments
 (0)