-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add support for aspect ratio in gemini image generation #3412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mwildehahn
wants to merge
6
commits into
pydantic:main
Choose a base branch
from
mwildehahn:mh/aspect-ratio-control
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−8
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd1b39b
Add support for aspect ratio in gemini image generation
mwildehahn 9a3d8b0
Update snapshot
mwildehahn 3ecda0e
Add test to examples
mwildehahn 1d8d19b
Review feedback
mwildehahn ab0730c
Review feedback re: openai
mwildehahn 6037a66
Merge branch 'main' into mh/aspect-ratio-control
DouweM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -202,7 +202,7 @@ The [`ImageGenerationTool`][pydantic_ai.builtin_tools.ImageGenerationTool] enabl | |||||
| | Provider | Supported | Notes | | ||||||
| |----------|-----------|-------| | ||||||
| | 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]. | | ||||||
| | Google | ✅ | No 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). These models will always generate images, even if this built-in tool is not explicitly specified. | | ||||||
| | 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. | | ||||||
| | Anthropic | ❌ | | | ||||||
| | Groq | ❌ | | | ||||||
| | Bedrock | ❌ | | | ||||||
|
|
@@ -291,6 +291,27 @@ assert isinstance(result.output, BinaryImage) | |||||
|
|
||||||
| _(This example is complete, it can be run "as is")_ | ||||||
|
|
||||||
| OpenAI Responses models also respect the `aspect_ratio` parameter. Because the OpenAI API only exposes discrete image sizes, | ||||||
| PydanticAI maps `'1:1'` -> `1024x1024`, `'2:3'` -> `1024x1536`, and `'3:2'` -> `1536x1024`. Providing any other aspect ratio | ||||||
| results in an error, and if you also set `size` it must match the computed value. | ||||||
|
|
||||||
| To control the aspect ratio when using Gemini image models, include the `ImageGenerationTool` explicitly: | ||||||
|
|
||||||
| ```py {title="image_generation_google_aspect_ratio.py"} | ||||||
| from pydantic_ai import Agent, BinaryImage, ImageGenerationTool | ||||||
|
|
||||||
| agent = Agent( | ||||||
| 'google-gla:gemini-2.5-flash-image', | ||||||
| builtin_tools=[ImageGenerationTool(aspect_ratio='16:9')], | ||||||
| output_type=BinaryImage, | ||||||
| ) | ||||||
|
|
||||||
| result = agent.run_sync('Generate a wide illustration of an axolotl city skyline.') | ||||||
| assert isinstance(result.output, BinaryImage) | ||||||
| ``` | ||||||
|
|
||||||
| _(This example is complete, it can be run "as is")_ | ||||||
|
|
||||||
| For more details, check the [API documentation][pydantic_ai.builtin_tools.ImageGenerationTool]. | ||||||
|
|
||||||
| #### Provider Support | ||||||
|
|
@@ -305,6 +326,7 @@ For more details, check the [API documentation][pydantic_ai.builtin_tools.ImageG | |||||
| | `partial_images` | ✅ | ❌ | | ||||||
| | `quality` | ✅ | ❌ | | ||||||
| | `size` | ✅ | ❌ | | ||||||
| | `aspect_ratio` | ✅ (1:1, 2:3, 3:2) | ✅ | | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Ideally we'd use a different emoji for partial support; any ideas? 😄 |
||||||
|
|
||||||
| ## URL Context Tool | ||||||
|
|
||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.