-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Conversation
e7b6dec to
9a3d8b0
Compare
docs/builtin-tools.md
Outdated
|
|
||
| _(This example is complete, it can be run "as is")_ | ||
|
|
||
| To control the aspect ratio when using Gemini image models, include the `ImageGenerationTool` explicitly: |
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.
Move this example under "Configuration Options" please
| |----------|-----------|-------| | ||
| | 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 | ✅ | 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. | |
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.
I'd say "Limited parameter support" like we do further up for web search.
| Supported by: | ||
| * Google image-generation models (Gemini) when the tool is explicitly enabled. |
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.
I think we can drop "when the tool is explicitly enabled." as that's implied by this being on that builtin tool class.
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.
I wonder if we could support some of these values for OpenAI as well by mapping to one of the size options. Then we'd only need to raise an error from OpenAI if another value is used, or if size and aspect_ratio are used at the same time.
| ) | ||
| if tool.aspect_ratio: | ||
| if image_config and image_config.get('aspect_ratio') != tool.aspect_ratio: | ||
| raise UserError( |
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.
We really only support a single instance anyway, so we can drop this and just always set image_config
| response_schema=response_schema, | ||
| response_modalities=modalities, | ||
| ) | ||
| config: GenerateContentConfigDict = { |
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.
Why did we have to change how this is built?
DouweM
left a comment
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.
@mwildehahn Thanks for doing the OpenAI Responses support, that's a nice quality of life detail. Just 2 points on docs and then we'll merge!
docs/builtin-tools.md
Outdated
| | `partial_images` | ✅ | ❌ | | ||
| | `quality` | ✅ | ❌ | | ||
| | `size` | ✅ | ❌ | | ||
| | `aspect_ratio` | ✅ (1:1, 2:3, 3:2) | ✅ | |
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.
| | `aspect_ratio` | ✅ (1:1, 2:3, 3:2) | ✅ | | |
| | `aspect_ratio` | ✅ (`1:1`, `2:3`, `3:2`) | ✅ | |
Ideally we'd use a different emoji for partial support; any ideas? 😄
|
This PR is stale, and will be closed in 3 days if no reply is received. |
|
@mwildehahn Reminder to look at 2 small changes here! |
|
Excited for this feature! Thank you for putting it together |
d09bb8b to
f2f2cd6
Compare
|
@DouweM done |
| tool_config=tool_config, | ||
| response_mime_type=response_mime_type, | ||
| response_json_schema=response_schema, | ||
| response_schema=response_schema, |
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.
Please revert this change!
|
Thanks @mwildehahn! You accidentally changed |
|
Ah sorry! All good, sorry I was slow on this.
…On Tue, Dec 9, 2025 at 4:05 PM Douwe Maan ***@***.***> wrote:
*DouweM* left a comment (pydantic/pydantic-ai#3412)
<#3412 (comment)>
Thanks @mwildehahn <https://github.com/mwildehahn>! You accidentally
changed response_json_schema to response_schema thought which caused a
few tests to fail 😅 @ajac-zero <https://github.com/ajac-zero> also fixed
up your PR yesterday in #3672
<#3672> which is essentially
identical but does pass CI, so I'll merge that one. I'll make sure you're
credited in the changelog :)
—
Reply to this email directly, view it on GitHub
<#3412 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFMAUMQBATOZZK4WJ2K2OL4A5PU7AVCNFSM6AAAAACMAW4BTGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTMMZUG44TEMJSGA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Fix for #3119