Skip to content

fix(image): pass aspect ratio and size to Gemini Flash image models - #4656

Merged
Re-bin merged 4 commits into
HKUDS:mainfrom
stupidloud:fix/gemini-flash-aspect-ratio
Jul 26, 2026
Merged

Re-bin merged 4 commits into
HKUDS:mainfrom
stupidloud:fix/gemini-flash-aspect-ratio

Conversation

@stupidloud

Copy link
Copy Markdown
Contributor

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 forwarded aspect_ratio / image_size to _generate_gemini_flash, and that method didn't accept them. As a result every Flash request went out with only responseModalities, 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 handled aspectRatio and is unaffected.

Fix

Forward the hints and emit them under generationConfig.responseFormat.image, per the current Gemini API:

"generationConfig": {
  "responseModalities": ["TEXT", "IMAGE"],
  "responseFormat": { "image": { "aspectRatio": "16:9", "imageSize": "2K" } }
}
  • aspectRatio is validated against the set Gemini Flash accepts; unsupported values are ignored rather than sent.
  • imageSize is validated against {512, 1K, 2K, 4K} (values like 1024x1024 are skipped) and only sent to Gemini 3+ image models, since gemini-2.5-flash-image supports only aspectRatio.
  • When no valid hint applies, responseFormat is omitted entirely, preserving the previous request shape.

Tests

Added three cases to tests/providers/test_image_generation.py:

  • Gemini 3 image model forwards both aspectRatio and imageSize
  • gemini-2.5-flash-image forwards aspectRatio but drops imageSize
  • unsupported hints (7:5, 1024x1024) omit responseFormat

Full suite passes (68) and ruff check is clean.

🤖 Generated with Claude Code

@chengyongru chengyongru left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread nanobot/providers/image_generation.py Outdated
Comment thread nanobot/providers/image_generation.py Outdated
@chengyongru chengyongru added priority: p2 Normal backlog: minor bug, enhancement, docs, cleanup, edge case, or unvalidated proposal. bug Something isn't working provider labels Jul 3, 2026
stupidloud and others added 4 commits July 27, 2026 02:58
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>
@Re-bin
Re-bin force-pushed the fix/gemini-flash-aspect-ratio branch from 8d57e6c to 92e1656 Compare July 26, 2026 19:00

@Re-bin Re-bin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 passed
  • python -m ruff check nanobot/providers/image_generation.py tests/providers/test_image_generation.py tests/tools/test_image_generation_tool.py — passed
  • git 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

@Re-bin
Re-bin merged commit cf1e801 into HKUDS:main Jul 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working priority: p2 Normal backlog: minor bug, enhancement, docs, cleanup, edge case, or unvalidated proposal. provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants