Skip to content

fix(vertex_ai): reject n > 1 for Gemini image models (#53119) - #53201

Closed
Aniketchavan7 wants to merge 1 commit into
BerriAI:mainfrom
Aniketchavan7:fix/vertex-gemini-image-n-rejection
Closed

Aniketchavan7 wants to merge 1 commit into
BerriAI:mainfrom
Aniketchavan7:fix/vertex-gemini-image-n-rejection

Conversation

@Aniketchavan7

Copy link
Copy Markdown

Fixes #53119

What changes are being made and why?

On litellm, VertexGeminiConfig.map_openai_params maps OpenAI n to Vertex candidateCount for all Gemini models. However, Vertex Gemini image models (gemini-3.1-flash-image, gemini-nano-banana-2.1, gemini-2.5-flash-image) reject candidateCount > 1 with an opaque Vertex error:

400 INVALID_ARGUMENT: Request contains an invalid argument.

leaving callers with no indication that n caused the failure.

Key Changes

  1. _is_gemini_image_model(model):
    • Checks supported_output_modalities in litellm.model_cost across candidates (model, f"{self._route}/{bare}", f"gemini/{bare}", bare).
    • Returns True if "image" is among the supported output modalities.
  2. VertexGeminiConfig.map_openai_params:
    • When param == "n" and value > 1 for an image model, immediately raises a clear litellm.BadRequestError:
      n={value} is not supported: Vertex Gemini image models return one image per request; send n=1 or separate requests.
    • Leaves n: 1 or standard text Gemini models untouched (optional_params["candidate_count"] = value).
  3. Regression Tests:
    • Added test_vertex_gemini_image_model_rejects_candidate_count_greater_than_one in tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py verifying that n > 1 on an image model raises BadRequestError, n == 1 sets candidate_count = 1, and n == 2 on text models (gemini-1.5-pro) maps as expected.

How was this tested?

  • Tested with gemini-3.1-flash-image, gemini-nano-banana-2.1, gemini-2.5-flash-image (raises 400).
  • Tested n=1 on image model (passes).
  • Tested n=2 on gemini-1.5-pro (passes, sets candidate_count=2).
  • Ran regression test suite; clean pass.

Co-authored-by: yalongwastaken yalongwastaken@users.noreply.github.com

@CLAassistant

CLAassistant commented Nov 20, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@veria-ai

veria-ai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

On Vertex Gemini image models (models with 'image' in supported_output_modalities, such as gemini-3.1-flash-image and gemini-nano-banana-2.1), setting n > 1 forwards candidateCount > 1 to Vertex, which responds with an opaque '400 INVALID_ARGUMENT: Request contains an invalid argument'.

This patch:
- Adds _is_gemini_image_model(model) checking supported_output_modalities in model_cost.
- Rejects n > 1 with a clear BadRequestError: 'n={value} is not supported: Vertex Gemini image models return one image per request; send n=1 or separate requests.' before forwarding to Vertex.
- Adds regression test test_vertex_gemini_image_model_rejects_candidate_count_greater_than_one in test_vertex_and_google_ai_studio_gemini.py.
@github-actions

Copy link
Copy Markdown
Contributor

#53119 was fixed by #53598 on main, so this pull request is closed. Reopen it if something was missed.

@github-actions github-actions Bot closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Vertex Gemini image models: n > 1 on /chat/completions is forwarded as candidateCount and returns an opaque 400 INVALID_ARGUMENT

2 participants