Skip to content

Commit 1d8d19b

Browse files
committed
Review feedback
1 parent 3ecda0e commit 1d8d19b

File tree

4 files changed

+40
-63
lines changed

4 files changed

+40
-63
lines changed

docs/builtin-tools.md

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ The [`ImageGenerationTool`][pydantic_ai.builtin_tools.ImageGenerationTool] enabl
202202
| Provider | Supported | Notes |
203203
|----------|-----------|-------|
204204
| OpenAI Responses || Full feature support. Only supported by models newer than `gpt-5`. Metadata about the generated image, like the [`revised_prompt`](https://platform.openai.com/docs/guides/tools-image-generation#revised-prompt) sent to the underlying image model, is available on the [`BuiltinToolReturnPart`][pydantic_ai.messages.BuiltinToolReturnPart] that's available via [`ModelResponse.builtin_tool_calls`][pydantic_ai.messages.ModelResponse.builtin_tool_calls]. |
205-
| Google || Supports the `aspect_ratio` parameter when explicitly provided. Only supported by [image generation models](https://ai.google.dev/gemini-api/docs/image-generation) like `gemini-2.5-flash-image`. These models do not support [structured output](output.md) or [function tools](tools.md) and will always generate images, even if this built-in tool is not explicitly specified. |
205+
| Google || Limited parameter support. Only supported by [image generation models](https://ai.google.dev/gemini-api/docs/image-generation) like `gemini-2.5-flash-image`. These models do not support [structured output](output.md) or [function tools](tools.md) and will always generate images, even if this built-in tool is not explicitly specified. |
206206
| Anthropic || |
207207
| Groq || |
208208
| Bedrock || |
@@ -248,23 +248,6 @@ assert isinstance(result.response.images[0], BinaryImage)
248248

249249
_(This example is complete, it can be run "as is")_
250250

251-
To control the aspect ratio when using Gemini image models, include the `ImageGenerationTool` explicitly:
252-
253-
```py {title="image_generation_google_aspect_ratio.py"}
254-
from pydantic_ai import Agent, BinaryImage, ImageGenerationTool
255-
256-
agent = Agent(
257-
'google-gla:gemini-2.5-flash-image',
258-
builtin_tools=[ImageGenerationTool(aspect_ratio='16:9')],
259-
output_type=BinaryImage,
260-
)
261-
262-
result = agent.run_sync('Generate a wide illustration of an axolotl city skyline.')
263-
assert isinstance(result.output, BinaryImage)
264-
```
265-
266-
_(This example is complete, it can be run "as is")_
267-
268251
The `ImageGenerationTool` can be used together with `output_type=BinaryImage` to get [image output](output.md#image-output). If the `ImageGenerationTool` built-in tool is not explicitly specified, it will be enabled automatically:
269252

270253
```py {title="image_generation_output.py"}
@@ -308,7 +291,22 @@ assert isinstance(result.output, BinaryImage)
308291

309292
_(This example is complete, it can be run "as is")_
310293

311-
Gemini image models support the separate `aspect_ratio` parameter; set `aspect_ratio='16:9'`, for example, when calling `ImageGenerationTool` to generate wide or tall compositions.
294+
To control the aspect ratio when using Gemini image models, include the `ImageGenerationTool` explicitly:
295+
296+
```py {title="image_generation_google_aspect_ratio.py"}
297+
from pydantic_ai import Agent, BinaryImage, ImageGenerationTool
298+
299+
agent = Agent(
300+
'google-gla:gemini-2.5-flash-image',
301+
builtin_tools=[ImageGenerationTool(aspect_ratio='16:9')],
302+
output_type=BinaryImage,
303+
)
304+
305+
result = agent.run_sync('Generate a wide illustration of an axolotl city skyline.')
306+
assert isinstance(result.output, BinaryImage)
307+
```
308+
309+
_(This example is complete, it can be run "as is")_
312310

313311
For more details, check the [API documentation][pydantic_ai.builtin_tools.ImageGenerationTool].
314312

pydantic_ai_slim/pydantic_ai/builtin_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class ImageGenerationTool(AbstractBuiltinTool):
263263
264264
Supported by:
265265
266-
* Google image-generation models (Gemini) when the tool is explicitly enabled.
266+
* Google image-generation models (Gemini)
267267
"""
268268

269269
kind: str = 'image_generation'

pydantic_ai_slim/pydantic_ai/models/google.py

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,6 @@ def _get_tools(
353353
"`ImageGenerationTool` is not supported by this model. Use a model with 'image' in the name instead."
354354
)
355355
if tool.aspect_ratio:
356-
if image_config and image_config.get('aspect_ratio') != tool.aspect_ratio:
357-
raise UserError(
358-
'Multiple `ImageGenerationTool` instances with different `aspect_ratio` values are not supported.'
359-
)
360356
image_config = ImageConfigDict(aspect_ratio=tool.aspect_ratio)
361357
else: # pragma: no cover
362358
raise UserError(
@@ -448,29 +444,28 @@ async def _build_content_and_config(
448444
else:
449445
raise UserError('Google does not support setting ModelSettings.timeout to a httpx.Timeout')
450446

451-
config: GenerateContentConfigDict = {
452-
'http_options': http_options,
453-
'system_instruction': system_instruction,
454-
'temperature': model_settings.get('temperature'),
455-
'top_p': model_settings.get('top_p'),
456-
'max_output_tokens': model_settings.get('max_tokens'),
457-
'stop_sequences': model_settings.get('stop_sequences'),
458-
'presence_penalty': model_settings.get('presence_penalty'),
459-
'frequency_penalty': model_settings.get('frequency_penalty'),
460-
'seed': model_settings.get('seed'),
461-
'safety_settings': model_settings.get('google_safety_settings'),
462-
'thinking_config': model_settings.get('google_thinking_config'),
463-
'labels': model_settings.get('google_labels'),
464-
'media_resolution': model_settings.get('google_video_resolution'),
465-
'cached_content': model_settings.get('google_cached_content'),
466-
'tools': cast(ToolListUnionDict, tools),
467-
'tool_config': tool_config,
468-
'response_mime_type': response_mime_type,
469-
'response_schema': response_schema,
470-
'response_modalities': modalities,
471-
}
472-
if image_config:
473-
config['image_config'] = image_config
447+
config = GenerateContentConfigDict(
448+
http_options=http_options,
449+
system_instruction=system_instruction,
450+
temperature=model_settings.get('temperature'),
451+
top_p=model_settings.get('top_p'),
452+
max_output_tokens=model_settings.get('max_tokens'),
453+
stop_sequences=model_settings.get('stop_sequences'),
454+
presence_penalty=model_settings.get('presence_penalty'),
455+
frequency_penalty=model_settings.get('frequency_penalty'),
456+
seed=model_settings.get('seed'),
457+
safety_settings=model_settings.get('google_safety_settings'),
458+
thinking_config=model_settings.get('google_thinking_config'),
459+
labels=model_settings.get('google_labels'),
460+
media_resolution=model_settings.get('google_video_resolution'),
461+
cached_content=model_settings.get('google_cached_content'),
462+
tools=cast(ToolListUnionDict, tools),
463+
tool_config=tool_config,
464+
response_mime_type=response_mime_type,
465+
response_schema=response_schema,
466+
response_modalities=modalities,
467+
image_config=image_config,
468+
)
474469

475470
return contents, config
476471

tests/models/test_google.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3143,22 +3143,6 @@ async def test_google_image_generation_tool_aspect_ratio(google_provider: Google
31433143
assert image_config == {'aspect_ratio': '16:9'}
31443144

31453145

3146-
async def test_google_image_generation_tool_aspect_ratio_conflict(google_provider: GoogleProvider) -> None:
3147-
model = GoogleModel('gemini-2.5-flash-image', provider=google_provider)
3148-
params = ModelRequestParameters(
3149-
builtin_tools=[
3150-
ImageGenerationTool(aspect_ratio='16:9'),
3151-
ImageGenerationTool(aspect_ratio='1:1'),
3152-
]
3153-
)
3154-
3155-
with pytest.raises(
3156-
UserError,
3157-
match='Multiple `ImageGenerationTool` instances with different `aspect_ratio` values are not supported.',
3158-
):
3159-
model._get_tools(params) # pyright: ignore[reportPrivateUsage]
3160-
3161-
31623146
async def test_google_vertexai_image_generation(allow_model_requests: None, vertex_provider: GoogleProvider):
31633147
model = GoogleModel('gemini-2.5-flash-image', provider=vertex_provider)
31643148

0 commit comments

Comments
 (0)