Repository navigation
fix(image): pass aspect ratio and size to Gemini Flash image models - #4656
Conversation
chengyongru
left a comment
There was a problem hiding this comment.
Automated PR review by nanobot. This is not a human maintainer review or approval.
I found model-capability checks that are broader than the documented Gemini image-model matrix, so the new request fields can be sent to models where they are not documented as supported.
The Gemini Flash image path (`generateContent`) dropped both `aspect_ratio`
and `image_size`: `generate()` never forwarded them and
`_generate_gemini_flash` did not accept them, so every request fell back to
1:1 / input-matched output. The Imagen path was unaffected.
Forward the hints and emit them under
`generationConfig.responseFormat.image` per the current Gemini API. Aspect
ratio is validated against the accepted set; `imageSize` is validated against
{512,1K,2K,4K} and only sent to Gemini 3+ image models, since
`gemini-2.5-flash-image` supports only `aspectRatio`.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Address review feedback that the capability checks were broader than the documented per-model matrix: - Drop the extreme aspect ratios (1:4, 4:1, 1:8, 8:1) from the Flash allow-list. They are only documented for 3.1 Flash / Flash Lite, so the global set could send an unsupported ratio to 2.5 Flash Image or 3.1 Pro Image. Keep the ratios common to every Flash image model. - Identify imageSize support positively via "gemini-3" instead of excluding "2.5". The old predicate also matched gemini-2.0-flash-preview-image- generation, which (with the default 1K size) altered that model's request shape even though only Gemini 3+ image models accept a configurable size. Add tests for the gemini-2.0 image-size drop and the extreme-ratio drop. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
8d57e6c to
92e1656
Compare
Re-bin
left a comment
There was a problem hiding this comment.
Approved at exact head 92e1656ef6990088fe6885afe60cfb3255db7555 after rebasing onto current main (b695a7e87558785d319ed5eb9797a63852a8a2d0).
Reviewed the complete Gemini native-image request path against Google's current GenerateContent image-generation documentation. The request now forwards supported hints through generationConfig.responseFormat.image, keeps imageSize off Gemini 2.0/2.5 and non-image models, scopes 1K/2K/4K/512 support to the documented Gemini 3 image families, and permits the four extreme aspect ratios only for Gemini 3.1 Flash Image and Flash Lite while rejecting them for Gemini 3 Pro Image and Gemini 2.5 Flash Image. Requests without an applicable hint preserve the old generationConfig shape.
Verification:
python -m pytest tests/providers/test_image_generation.py tests/tools/test_image_generation_tool.py -q— 100 passedpython -m ruff check nanobot/providers/image_generation.py tests/providers/test_image_generation.py tests/tools/test_image_generation_tool.py— passedgit diff --check— passed- Exact-head Test Suite run
30215938519— all 6 jobs passed (Detect changes, webui, docker, Python 3.11, Python 3.14 + coverage, Windows Python 3.14) - Both prior review threads are resolved; no unresolved current or outdated discussion remains
Problem
The Gemini image provider silently dropped aspect-ratio and size hints on the Flash (
generateContent) path — used by Nano Banana (gemini-2.5-flash-image) and the Gemini 3 image models.GeminiImageGenerationClient.generate()never forwardedaspect_ratio/image_sizeto_generate_gemini_flash, and that method didn't accept them. As a result every Flash request went out with onlyresponseModalities, so the model always fell back to 1:1 (or input-matched) output regardless of what the user requested. The Imagen path (_generate_imagen) already handledaspectRatioand is unaffected.Fix
Forward the hints and emit them under
generationConfig.responseFormat.image, per the current Gemini API:aspectRatiois validated against the set Gemini Flash accepts; unsupported values are ignored rather than sent.imageSizeis validated against{512, 1K, 2K, 4K}(values like1024x1024are skipped) and only sent to Gemini 3+ image models, sincegemini-2.5-flash-imagesupports onlyaspectRatio.responseFormatis omitted entirely, preserving the previous request shape.Tests
Added three cases to
tests/providers/test_image_generation.py:aspectRatioandimageSizegemini-2.5-flash-imageforwardsaspectRatiobut dropsimageSize7:5,1024x1024) omitresponseFormatFull suite passes (68) and
ruff checkis clean.🤖 Generated with Claude Code