Skip to content

fix(vertex_ai): answer a clear 400 for n > 1 with image modalities on Gemini chat to lts/1.108.x - #53752

Merged
mateo-berri merged 6 commits into
BerriAI:lts/1.108.xfrom
izzybn:litellm_vertex_gemini_image_n_rejection_lts
Nov 23, 2026
Merged

mateo-berri merged 6 commits into
BerriAI:lts/1.108.xfrom
izzybn:litellm_vertex_gemini_image_n_rejection_lts

Conversation

@izzybn

@izzybn izzybn commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Handwritten 1-3 sentence description

On the 1.108 LTS line, asking a Vertex Gemini image model for more than one answer while also setting modalities fails with Google's vague "Request contains an invalid argument." error. This brings main's fix for it to the line, so the proxy turns that request down with a 400 that says what to change, or drops the extra answers when drop_params is on

Details

TLDR

Problem this solves:

  • Vertex image models answer n > 1 with modalities with only "Request contains an invalid argument.", on /v1/chat/completions and /v1/responses

How it solves it:

  • n > 1 with "image" in modalities answers a 400 naming n
  • drop_params drops n with one warning
  • Batch rows keep forwarding n, so Vertex fails only that row
  • Every other request maps n as before
Backport checklist
  • Main's fix is c70e97c, its lint refactor bcbacc0, 291ed52 and 4f83f39, which together keep batch rows with n uploading and name the provider only on the n error. The line gets the first two as one commit, c52feeb, and the other two as 3ca89df and a9ec78b, as the table below lists
  • Adapted to the line: the line never passes allowed_openai_params into the Gemini param mapping (mapper, Gemini call), so main's allowed_openai_params: ["n"] branch and its test row are left out.
  • Merging lts/1.108.x in brought its batch mapper to keep row and deployment drop_params, so the line runs main's batch test for all three
  • The picked tests pass on the line, and the new cases fail without the fix
  • No dependency moves
  • No enterprise/ or litellm-proxy-extras/ change, so no package bump
Main commit LTS commit
c70e97ccd5 c52feeb27a
bcbacc0997 c52feeb27a
291ed5279f 3ca89dfd22
4f83f3978f a9ec78b091

User Flow

Before: a developer asking a Vertex Gemini image model for two images in one request gets a vague 400

  1. They send POST https://litellm-domain/v1/chat/completions with model vertex_ai/gemini-3.1-flash-image, "modalities": ["image", "text"] and "n": 2
  2. Vertex answers 400 "Request contains an invalid argument.", never naming n
  3. POST https://litellm-domain/v1/responses fails the same way

After: the same request gets a 400 that names n, or one image under drop_params

  1. They send the same request
  2. The proxy answers 400 at once, naming n and how to fix it
  3. With "drop_params": true it returns 200 with one image, on /v1/responses too
  4. Without modalities, "n": 2 still returns two images

Relevant issues

Part of #53119

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Two proxies ran from the same config, each with 2 uvicorn workers and its own Postgres DB. Before is lts/1.108.x at 9fca077 and After is bce9301. Every request hit real Vertex AI or Google AI Studio. Raw output is in this gist, files 27 to 29

Last updated: bce9301

Shared setup (config, env var names, start command)
model_list:
  - model_name: vertex-flash-image
    litellm_params:
      model: vertex_ai/gemini-3.1-flash-image
      vertex_project: os.environ/VERTEXAI_PROJECT
      vertex_location: global
  - model_name: vertex-nano-banana
    litellm_params:
      model: vertex_ai/gemini-nano-banana-2.1
      vertex_project: os.environ/VERTEXAI_PROJECT
      vertex_location: global
  - model_name: studio-flash-image
    litellm_params:
      model: gemini/gemini-3.1-flash-image
      api_key: os.environ/GEMINI_API_KEY
  - model_name: vertex-text
    litellm_params:
      model: vertex_ai/gemini-3-flash-preview
      vertex_project: os.environ/VERTEXAI_PROJECT
      vertex_location: global
  - model_name: vertex-25-flash-image
    litellm_params:
      model: vertex_ai/gemini-2.5-flash-image
      vertex_project: os.environ/VERTEXAI_PROJECT
      vertex_location: global

Env vars: VERTEXAI_PROJECT, GOOGLE_APPLICATION_CREDENTIALS, GEMINI_API_KEY, LITELLM_MASTER_KEY, DATABASE_URL

python litellm/proxy/proxy_cli.py --config config.yaml --host 127.0.0.1 --port <port> --num_workers 2 --use_prisma_db_push

Every case is one curl, printed by the shell from the same string it ran

# Request (chat, Vertex flash-image, image modalities unless noted) Before (9fca077) After (bce9301)
1 n=2 vague 400 400 names n
2 n=2, drop_params vague 400 200, 1 image
3 nano-banana, n=2 vague 400 400 names n
4 nano-banana, n=2, drop_params vague 400 200, 1 image
5 AI Studio flash-image, n=2 400 "Multiple candidates" 400 names n
6 AI Studio flash-image, n=2, drop_params 400 "Multiple candidates" 200, 1 image
7 n=2, stream vague 400 400 names n
8 n=2, drop_params, stream vague 400 200, 1 image
9 modalities [image] before n=2 vague 400 400 names n
13 n=2, allowed_openai_params: ["n"] vague 400 400 names n
15 Responses, n=2 vague 400 400 names n
16 Responses, n=2, drop_params vague 400 200, 1 image
17 Responses, n=2, stream 200 then response.failed 400 names n
18 modalities [text, image], n=2 vague 400 400 names n
19 n=2, allowed_openai_params: ["n"], drop_params vague 400 200, 1 image
Cases with the same answer on both sides
# Request (chat, Vertex flash-image, image modalities unless noted) Before (9fca077) After (bce9301)
10 n=1 200, 1 image 200, 1 image
11 no modalities, n=2 200, 2 images 200, 2 images
12 text modalities, n=2 vague 400 vague 400
14 text model, text modalities, n=2 200, 2 choices 200, 2 choices
20 2.5-flash-image, no modalities, n=2 400 "not supported" 400 "not supported"
21 AI Studio flash-image, no modalities, n=2, drop_params 400 "Multiple candidates" 400 "Multiple candidates"
22 native generateContent, candidateCount 2, IMAGE modality vague 400 vague 400

Case 13 is the one place the line differs from main. The line never passes allowed_openai_params to this mapping, so that request gets the clear 400 here, where main forwards n and Vertex answers its vague 400

A Vertex batch file with an n=2 image row uploads on both sides, so the line keeps sending that row to Vertex, which fails only that row (file 29)

Before (9fca077)

  1. Ask for two images with image modalities
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2}'
{"error":{"message":"litellm.BadRequestError: Vertex_aiException BadRequestError - {\n  \"error\": {\n    \"code\": 400,\n    \"message\": \"Request contains an invalid argument.\",\n    \"status\": \"INVALID_ARGUMENT\"\n  }\n}\n\n\nLiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.","type":"invalid_request_error","param":null,"code":"400"}}
HTTP 400
All 22 cases, Before
== 1. vertex-flash-image, image modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "Request contains an invalid argument.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 2. vertex-flash-image, image modalities, n=2, drop_params
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2, "drop_params": true}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "Request contains an invalid argument.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 3. vertex-nano-banana, image modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-nano-banana", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "Request contains an invalid argument.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-nano-banana' failed with the error above. No fallback was attempted.
== 4. vertex-nano-banana, image modalities, n=2, drop_params
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-nano-banana", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2, "drop_params": true}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "Request contains an invalid argument.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-nano-banana' failed with the error above. No fallback was attempted.
== 5. studio-flash-image, image modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "studio-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2}'
HTTP 400
error: litellm.BadRequestError: GeminiException BadRequestError - {   "error": {     "code": 400,     "message": "Multiple candidates is not enabled for this model",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'studio-flash-image' failed with the error above. No fallback was attempted.
== 6. studio-flash-image, image modalities, n=2, drop_params
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "studio-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2, "drop_params": true}'
HTTP 400
error: litellm.BadRequestError: GeminiException BadRequestError - {   "error": {     "code": 400,     "message": "Multiple candidates is not enabled for this model",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'studio-flash-image' failed with the error above. No fallback was attempted.
== 7. vertex-flash-image, image modalities, n=2, stream
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2, "stream": true}'
HTTP 400
error: b'{\n  "error": {\n    "code": 400,\n    "message": "Request contains an invalid argument.",\n    "status": "INVALID_ARGUMENT"\n  }\n}\n'  LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 8. vertex-flash-image, image modalities, n=2, drop_params, stream
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2, "drop_params": true, "stream": true}'
HTTP 400
error: b'{\n  "error": {\n    "code": 400,\n    "message": "Request contains an invalid argument.",\n    "status": "INVALID_ARGUMENT"\n  }\n}\n'  LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 9. vertex-flash-image, modalities [image] before n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "modalities": ["image"], "n": 2, "messages": [{"role": "user", "content": "Draw a red circle"}]}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "Request contains an invalid argument.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 10. vertex-flash-image, image modalities, n=1
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 1}'
HTTP 200
choices: 1 | images per choice: [1]
== 11. vertex-flash-image, no modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "n": 2}'
HTTP 200
choices: 2 | images per choice: [1, 1]
== 12. vertex-flash-image, text modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["text"], "n": 2}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "Request contains an invalid argument.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 13. vertex-flash-image, image modalities, n=2, allowed_openai_params [n]
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2, "allowed_openai_params": ["n"]}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "Request contains an invalid argument.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 14. vertex-text, text modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-text", "messages": [{"role": "user", "content": "Say hi"}], "modalities": ["text"], "n": 2, "max_tokens": 200}'
HTTP 200
choices: 2 | images per choice: [0, 0]
== 15. Responses API, vertex-flash-image, image modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "input": "Draw a red circle", "modalities": ["image", "text"], "n": 2}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "Request contains an invalid argument.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 16. Responses API, vertex-flash-image, image modalities, n=2, drop_params
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "input": "Draw a red circle", "modalities": ["image", "text"], "n": 2, "drop_params": true}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "Request contains an invalid argument.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 17. Responses API, vertex-flash-image, image modalities, n=2, stream
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "input": "Draw a red circle", "modalities": ["image", "text"], "n": 2, "stream": true}'
HTTP 200
event: response.failed
data: {"type":"response.failed","response":{"id":"resp_e517d221a7cc464da1de276e0b5b707f","created_at":1795404578,"error":{"code":"invalid_request_error","message":"litellm.BadRequestError: Vertex_ai_betaException BadRequestError - b'{\\n  \"error\": {\\n    \"code\": 400,\\n  
== 18. vertex-flash-image, modalities [text, image], n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["text", "image"], "n": 2}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "Request contains an invalid argument.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 19. vertex-flash-image, image modalities, n=2, allowed_openai_params [n], drop_params
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2, "allowed_openai_params": ["n"], "drop_params": true}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "Request contains an invalid argument.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 20. vertex-25-flash-image, no modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-25-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "n": 2}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "The request is not supported by this model.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-25-flash-image' failed with the error above. No fallback was attempted.
== 21. studio-flash-image, no modalities, n=2, drop_params
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "studio-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "n": 2, "drop_params": true}'
HTTP 400
error: litellm.BadRequestError: GeminiException BadRequestError - {   "error": {     "code": 400,     "message": "Multiple candidates is not enabled for this model",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'studio-flash-image' failed with the error above. No fallback was attempted.
== 22. vertex-flash-image, native generateContent, candidateCount 2, IMAGE modality
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:31424/v1beta/models/vertex-flash-image:generateContent -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"contents": [{"role": "user", "parts": [{"text": "Draw a red circle"}]}], "generationConfig": {"candidateCount": 2, "responseModalities": ["TEXT", "IMAGE"]}}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "Request contains an invalid argument.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.

After (bce9301)

  1. Same request
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2}'
{"error":{"message":"litellm.UnsupportedParamsError: n=2 with image output in modalities is not supported for gemini-3.1-flash-image. Gemini returns one candidate per request when modalities asks for image output, so send n=1 or separate requests, or set drop_params to drop n.\n\nLiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.","type":"invalid_request_error","param":null,"code":"400"}}
HTTP 400
All 22 cases, After
== 1. vertex-flash-image, image modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2}'
HTTP 400
error: litellm.UnsupportedParamsError: n=2 with image output in modalities is not supported for gemini-3.1-flash-image. Gemini returns one candidate per request when modalities asks for image output, so send n=1 or separate requests, or set drop_params to drop n.  LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 2. vertex-flash-image, image modalities, n=2, drop_params
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2, "drop_params": true}'
HTTP 200
choices: 1 | images per choice: [1]
== 3. vertex-nano-banana, image modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-nano-banana", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2}'
HTTP 400
error: litellm.UnsupportedParamsError: n=2 with image output in modalities is not supported for gemini-nano-banana-2.1. Gemini returns one candidate per request when modalities asks for image output, so send n=1 or separate requests, or set drop_params to drop n.  LiteLLM: model group 'vertex-nano-banana' failed with the error above. No fallback was attempted.
== 4. vertex-nano-banana, image modalities, n=2, drop_params
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-nano-banana", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2, "drop_params": true}'
HTTP 200
choices: 1 | images per choice: [1]
== 5. studio-flash-image, image modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "studio-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2}'
HTTP 400
error: litellm.UnsupportedParamsError: n=2 with image output in modalities is not supported for gemini-3.1-flash-image. Gemini returns one candidate per request when modalities asks for image output, so send n=1 or separate requests, or set drop_params to drop n.  LiteLLM: model group 'studio-flash-image' failed with the error above. No fallback was attempted.
== 6. studio-flash-image, image modalities, n=2, drop_params
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "studio-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2, "drop_params": true}'
HTTP 200
choices: 1 | images per choice: [1]
== 7. vertex-flash-image, image modalities, n=2, stream
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2, "stream": true}'
HTTP 400
error: litellm.UnsupportedParamsError: n=2 with image output in modalities is not supported for gemini-3.1-flash-image. Gemini returns one candidate per request when modalities asks for image output, so send n=1 or separate requests, or set drop_params to drop n.  LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 8. vertex-flash-image, image modalities, n=2, drop_params, stream
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2, "drop_params": true, "stream": true}'
HTTP 200
stream: 2 chunks, choice indexes [0], images 1
== 9. vertex-flash-image, modalities [image] before n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "modalities": ["image"], "n": 2, "messages": [{"role": "user", "content": "Draw a red circle"}]}'
HTTP 400
error: litellm.UnsupportedParamsError: n=2 with image output in modalities is not supported for gemini-3.1-flash-image. Gemini returns one candidate per request when modalities asks for image output, so send n=1 or separate requests, or set drop_params to drop n.  LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 10. vertex-flash-image, image modalities, n=1
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 1}'
HTTP 200
choices: 1 | images per choice: [1]
== 11. vertex-flash-image, no modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "n": 2}'
HTTP 200
choices: 2 | images per choice: [1, 1]
== 12. vertex-flash-image, text modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["text"], "n": 2}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "Request contains an invalid argument.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 13. vertex-flash-image, image modalities, n=2, allowed_openai_params [n]
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2, "allowed_openai_params": ["n"]}'
HTTP 400
error: litellm.UnsupportedParamsError: n=2 with image output in modalities is not supported for gemini-3.1-flash-image. Gemini returns one candidate per request when modalities asks for image output, so send n=1 or separate requests, or set drop_params to drop n.  LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 14. vertex-text, text modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-text", "messages": [{"role": "user", "content": "Say hi"}], "modalities": ["text"], "n": 2, "max_tokens": 200}'
HTTP 200
choices: 2 | images per choice: [0, 0]
== 15. Responses API, vertex-flash-image, image modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "input": "Draw a red circle", "modalities": ["image", "text"], "n": 2}'
HTTP 400
error: litellm.UnsupportedParamsError: n=2 with image output in modalities is not supported for gemini-3.1-flash-image. Gemini returns one candidate per request when modalities asks for image output, so send n=1 or separate requests, or set drop_params to drop n.  LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 16. Responses API, vertex-flash-image, image modalities, n=2, drop_params
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "input": "Draw a red circle", "modalities": ["image", "text"], "n": 2, "drop_params": true}'
HTTP 200
output types: ['image_generation_call'] | images: 1
== 17. Responses API, vertex-flash-image, image modalities, n=2, stream
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/responses -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "input": "Draw a red circle", "modalities": ["image", "text"], "n": 2, "stream": true}'
HTTP 400
error: litellm.UnsupportedParamsError: n=2 with image output in modalities is not supported for gemini-3.1-flash-image. Gemini returns one candidate per request when modalities asks for image output, so send n=1 or separate requests, or set drop_params to drop n.  LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 18. vertex-flash-image, modalities [text, image], n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["text", "image"], "n": 2}'
HTTP 400
error: litellm.UnsupportedParamsError: n=2 with image output in modalities is not supported for gemini-3.1-flash-image. Gemini returns one candidate per request when modalities asks for image output, so send n=1 or separate requests, or set drop_params to drop n.  LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.
== 19. vertex-flash-image, image modalities, n=2, allowed_openai_params [n], drop_params
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "modalities": ["image", "text"], "n": 2, "allowed_openai_params": ["n"], "drop_params": true}'
HTTP 200
choices: 1 | images per choice: [1]
== 20. vertex-25-flash-image, no modalities, n=2
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "vertex-25-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "n": 2}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "The request is not supported by this model.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-25-flash-image' failed with the error above. No fallback was attempted.
== 21. studio-flash-image, no modalities, n=2, drop_params
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"model": "studio-flash-image", "messages": [{"role": "user", "content": "Draw a red circle"}], "n": 2, "drop_params": true}'
HTTP 400
error: litellm.BadRequestError: GeminiException BadRequestError - {   "error": {     "code": 400,     "message": "Multiple candidates is not enabled for this model",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'studio-flash-image' failed with the error above. No fallback was attempted.
== 22. vertex-flash-image, native generateContent, candidateCount 2, IMAGE modality
$ curl -s -w '\nHTTP %{http_code}' http://127.0.0.1:21489/v1beta/models/vertex-flash-image:generateContent -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' -d '{"contents": [{"role": "user", "parts": [{"text": "Draw a red circle"}]}], "generationConfig": {"candidateCount": 2, "responseModalities": ["TEXT", "IMAGE"]}}'
HTTP 400
error: litellm.BadRequestError: Vertex_aiException BadRequestError - {   "error": {     "code": 400,     "message": "Request contains an invalid argument.",     "status": "INVALID_ARGUMENT"   } }   LiteLLM: model group 'vertex-flash-image' failed with the error above. No fallback was attempted.

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • Some n > 1 requests still get a provider 400 that never names n, as on base

Low

Decisions

The check reads the request, not the model map. Without modalities, gemini-3.1-flash-image returns two images for "n": 2 (case 11) while gemini-2.5-flash-image refuses it (case 20), yet the map's gemini/ rows list image output for both. A map rule like #53201's would break case 11, so the refusals without modalities, AI Studio's included (case 21), stay with the provider

Audio stays out of the check. gemini-2.5-flash-preview-tts returns two audio answers for "n": 2 while gemini-3.8-flash-tts fails, so a rule on audio alone would break a working request (file 36)

We rejected dropping responseModalities, which ignores the caller's modalities, and fanning n out, which multiplies cost unasked

allowed_openai_params: ["n"] wins over drop_params, the same order this file uses for logprobs

Batch rows with n > 1 still upload, since Vertex fails only that row

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR
Bugbot summary

[!NOTE]
Medium Risk
Changes request validation and param mapping on a hot path (Gemini chat and batch upload), but scope is limited to the n + image modalities case; batch intentionally bypasses the early error so Vertex still fails individual rows.

Overview
Gemini chat completion now validates the combination of n > 1 and modalities including "image" during OpenAI→Gemini param mapping. Interactive calls (vertex_ai and Google AI Studio) get a 400 UnsupportedParamsError that names n and suggests n=1, separate requests, or drop_params, instead of passing through to Google’s vague invalid-argument error.

When drop_params is enabled, n is omitted from the Gemini candidate_count mapping and a one-time warning is logged. n still maps normally when image output is not requested (e.g. no image in modalities).

Vertex batch JSONL upload passes allow_unsupported_n=True into the same mapper so problematic rows still upload with candidate_count; only online paths enforce the early 400. Unit tests cover mapper behavior, full completion flows, and batch row generation with and without drop_params.

Reviewed by Cursor Bugbot for commit bce9301. Bugbot is set up for automated code reviews on this repo. Configure here.

…_gemini_image_n_rejection_lts

# Conflicts:
#	litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py
#	tests/unit/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py
…r only on the n error

Pick of the main fix onto lts/1.108.x. A Vertex batch row with n > 1 and image
modalities is forwarded again, so Vertex fails only that row instead of the
upload refusing the whole file. The Gemini configs no longer override
custom_llm_provider, so only the n error carries the provider name.
…_gemini_image_n_rejection_lts

# Conflicts:
#	litellm/llms/vertex_ai/files/transformation.py
@izzybn
izzybn requested a review from a team November 23, 2026 07:22
@veria-ai

veria-ai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

@surlymochan surlymochan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified the mechanics against the code. drops_unsupported_params (core_helpers.py:412) is true when the global litellm.drop_params is true or the per-call value normalises to true, so the drop path works for both global and per-request flags. The guard runs on the typed request params before the parameter loop, so modalities is still present when it is checked, and the allow_unsupported_n=True bypass in the files transformation is deliberate — the batch-row test pins candidate_count=2 being preserved there.

One nit: the guard only fires for isinstance(n, int). A non-int n (e.g. n=2.0, or "2" arriving from a proxy JSON body) bypasses the 400 and is forwarded as candidate_count, where Gemini rejects it upstream instead of here. Same for modalities given as a bare string "image" rather than a list. Worth coercing or noting, but not blocking.

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge

Summary

This backport rejects n > 1 with image modalities before calling Vertex AI or Google AI Studio. With drop_params, it drops n and warns instead

Batch uploads still forward n unless drop_params is enabled. Added tests cover parameter order, both providers, outgoing requests, and global, deployment, and row-level settings

izzybn explicitly records deferred cases involving audio, absent or text-only modalities, native generateContent, string or float n, raw responseModalities, and misleading advice for text models asked for images. The description also records the intentional LTS difference for allowed_openai_params

  • Gemini image requests with multiple answers now get a clear 400.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A["Map Gemini request"] --> B{"n > 1 and image modalities?"}
  B -- No --> C["Keep existing mapping"]
  B -- Yes --> D{"drop_params enabled?"}
  D -- Yes --> E["Drop n and warn"]
  D -- No --> F{"Batch upload?"}
  F -- Yes --> G["Forward n to Vertex"]
  F -- No --> H["Return clear 400"]
Loading

Reviews (1) · Last reviewed commit: "Merge remote-tracking branch 'origin/lts..." · Reviewed by Greptile

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit bce9301. Configure here.

@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri mateo-berri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@mateo-berri
mateo-berri merged commit 2510fa9 into BerriAI:lts/1.108.x Nov 23, 2026
65 checks passed
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.

3 participants