Skip to content

fix(gemini): accept top_logprobs on Google AI Studio models - #49514

Open
mateo-berri wants to merge 3 commits into
mainfrom
litellm_gemini_top_logprobs
Open

mateo-berri wants to merge 3 commits into
mainfrom
litellm_gemini_top_logprobs

Conversation

@mateo-berri

@mateo-berri mateo-berri commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Handwritten 1-3 sentence description

Details

TLDR

Problem this solves:

  • top_logprobs on gemini/ models returned a 400 unless drop_params hid the problem
  • Vertex AI accepted the same parameter, so behavior differed by provider prefix
  • /v1/responses with top_logprobs built a Gemini body with logprobs but no responseLogprobs, which Gemini and Vertex both reject with their own 400

How it solves it:

  • Add top_logprobs to GoogleAIStudioGeminiConfig.get_supported_openai_params
  • VertexGeminiConfig.map_openai_params sets responseLogprobs: true when it maps top_logprobs
  • Integration cases for /v1/chat/completions and /v1/responses assert generationConfig.logprobs and generationConfig.responseLogprobs on the wire, plus unit tests for both param combinations

User Flow

Before: a developer asking Gemini for per-token alternatives gets an error instead of logprobs

  1. They send POST https://litellm-domain/v1/chat/completions for gemini/gemini-3.8-flash with "logprobs": true, "top_logprobs": 3
  2. The gateway answers 400 litellm.UnsupportedParamsError: gemini does not support parameters: ['top_logprobs']
  3. They send POST https://litellm-domain/v1/responses for vertex_ai/gemini-2.5-flash with "top_logprobs": 3 and get Vertex's 400 logprobs is not supported without setting response_logprobs to be true

After: the same requests reach Gemini with the logprob count and the response_logprobs flag

  1. The chat request's Gemini body carries generationConfig.responseLogprobs: true and generationConfig.logprobs: 3, and Gemini answers for the model (on the test key every Google AI Studio model answers 400 Logprobs is not enabled for this model, which is Google's model gating and the same answer logprobs: true alone gets on main)
  2. The /v1/responses request on vertex_ai/gemini-2.5-flash returns 200 with the model's text

Caveats

The Responses API bridge does not copy chat logprobs into output_text.logprobs, so a /v1/responses call with top_logprobs now succeeds but the response carries no logprobs. That is pre-existing on Vertex and out of scope here

None of the Google AI Studio models available to the QA key have logprobs enabled, so the live Google AI Studio legs stop at Gemini's own Logprobs is not enabled for this model 400. The Vertex legs on gemini-2.5-flash prove the shared mapping end to end

Screenshots / Proof of Fix

Two live proxies from worktrees, no database, real Google AI Studio and Vertex AI calls. Base 6aa546303e on :4821, head e5a489ca31 on :4822 (restarted from that commit after the LIT011 marker landed, legs rerun)

Config for both

model_list:
  - model_name: gemini-3.8-flash
    litellm_params:
      model: gemini/gemini-3.8-flash
      api_key: os.environ/GEMINI_API_KEY
  - model_name: vertex-gemini-2.5-flash
    litellm_params:
      model: vertex_ai/gemini-2.5-flash
      vertex_credentials: /path/to/sa.json
      vertex_project: <project>
      vertex_location: global
general_settings:
  master_key: sk-qa
litellm_settings:
  drop_params: false

Before (6aa5463, :4821)

curl -s localhost:4821/v1/chat/completions -H 'Authorization: Bearer sk-qa' -H 'Content-Type: application/json' -d '{"model":"gemini-3.8-flash","messages":[{"role":"user","content":"Say hi"}],"logprobs":true,"top_logprobs":3,"max_tokens":20}'
HTTP 400: litellm.UnsupportedParamsError: gemini does not support parameters: ['top_logprobs'], for model=gemini-3.8-flash

curl -s localhost:4821/v1/responses -H 'Authorization: Bearer sk-qa' -H 'Content-Type: application/json' -d '{"model":"gemini-3.8-flash","input":"Say hi","top_logprobs":3,"max_output_tokens":20}'
HTTP 400: litellm.UnsupportedParamsError: gemini does not support parameters: ['top_logprobs'], for model=gemini-3.8-flash

curl -s localhost:4821/v1/responses -H 'Authorization: Bearer sk-qa' -H 'Content-Type: application/json' -d '{"model":"vertex-gemini-2.5-flash","input":"Say hi","top_logprobs":3,"max_output_tokens":300,"reasoning":{"effort":"none"}}'
HTTP 400: Vertex_aiException BadRequestError - "Unable to submit request because logprobs is not supported without setting response_logprobs to be true"

curl -s localhost:4821/v1/chat/completions -H 'Authorization: Bearer sk-qa' -H 'Content-Type: application/json' -d '{"model":"vertex-gemini-2.5-flash","messages":[{"role":"user","content":"Say hi"}],"logprobs":true,"top_logprobs":3,"max_tokens":300,"thinking":{"type":"disabled"}}'
HTTP 200: text='Hi there!' logprobs.content tokens=3 first={"token": "Hi", "logprob": -0.0011147637, "top_logprobs": [{"token": "Hi", "logprob": -0.0011147637}, {"token": "Hello", "logprob": -6.813142}, {"token": "Hey", "logprob": -12.915714}]}

curl -s localhost:4821/v1/chat/completions -H 'Authorization: Bearer sk-qa' -H 'Content-Type: application/json' -d '{"model":"gemini-3.8-flash","messages":[{"role":"user","content":"Say hi"}],"logprobs":true,"max_tokens":20}'
HTTP 400: GeminiException BadRequestError - "Logprobs is not enabled for this model"   (logprobs alone, already supported on main, same Google gating)

After (e5a489c, :4822)

curl -s localhost:4822/v1/chat/completions ... same chat body for gemini-3.8-flash with "logprobs":true,"top_logprobs":3
HTTP 400: GeminiException BadRequestError - {"code": 400, "message": "Logprobs is not enabled for this model", "status": "INVALID_ARGUMENT"}
  (litellm no longer rejects the request, Gemini receives it and answers with its model gating, the same answer as logprobs alone)

curl -s localhost:4822/v1/responses ... same Responses body for gemini-3.8-flash with "top_logprobs":3
HTTP 400: GeminiException BadRequestError - "Logprobs is not enabled for this model"
  (before the responseLogprobs fix this leg got "logprobs can only be specified if response_logprobs is true")

curl -s localhost:4822/v1/responses ... same Responses body for vertex-gemini-2.5-flash with "top_logprobs":3
HTTP 200: status=completed, output[0].content[0].text="Hi! How can I help you today?"

curl -s localhost:4822/v1/chat/completions ... same chat body for vertex-gemini-2.5-flash
HTTP 200: text='Hi!' logprobs.content tokens=2 first={"token": "Hi", "logprob": -0.0002368408, "top_logprobs": [{"token": "Hi", "logprob": -0.0002368408}, {"token": "Hello", "logprob": -8.423428}, {"token": "**", "logprob": -11.879239}]}
  (unchanged path, no regression)

Tests at e5a489c (the only change since ffb199d is a # rebind-ok marker, the unit and integration selections above were run on the ffb199d tree)

pytest tests/unit/llms/gemini/chat/test_transformation.py -k top_logprobs
2 passed

python tests/integration/run.py providers tests/integration/translation/chat_completions/params/test_chat_completions_params_gemini_logprobs.py tests/integration/translation/responses/params/test_responses_params_gemini_logprobs.py
2 passed in 34.21s

Mutation: removing optional_params["responseLogprobs"] = True makes test_top_logprobs_alone_enables_response_logprobs and the Responses integration case fail, removing "top_logprobs" from the supported list makes both integration cases and test_top_logprobs_maps_to_generation_config_logprobs fail

/live-pr-risk pass at e5a489c: dependents of map_openai_params are the Vertex and Google AI Studio chat and Responses paths above, A/B'd live at base and head with real providers, no regression on the chat path, Responses path goes from a provider 400 to 200

Type

🐛 Bug Fix

Link to Devin session: https://app.devin.ai/sessions/09cdd950360c40bfa37dbf9e3bd4d9c8
Open in Devin Desktop: https://app.devin.ai/desktop/session/09cdd950360c40bfa37dbf9e3bd4d9c8?variant=devin
Requested by: @mateo-berri


Note

Low Risk
Narrow request-parameter translation for Gemini logprobs with regression tests; no auth, billing, or data-path changes beyond fixing provider request shape.

Overview
Fixes top_logprobs being rejected on gemini/... routes and fixes Gemini generationConfig when only top_logprobs is sent (notably /v1/responses).

Google AI Studio now lists top_logprobs in get_supported_openai_params, so LiteLLM no longer raises UnsupportedParamsError before the existing Vertex/Gemini param mapper runs.

The shared VertexGeminiConfig.map_openai_params path still maps top_logprobs → generationConfig.logprobs, and now also sets responseLogprobs: true whenever top_logprobs is present, matching Gemini’s requirement that the logprob count not be sent without enabling response logprobs.

Unit tests cover both logprobs + top_logprobs and top_logprobs alone; integration translation tests assert the outgoing generationConfig for chat completions and Responses.

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

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@greptile-apps

greptile-apps Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge with no new actionable findings

Summary

Adds top_logprobs support for Google AI Studio and enables responseLogprobs when the shared Gemini mapper receives top_logprobs

Unit and integration tests cover chat requests and Responses requests. The only change since the previous review adds a rebind-ok comment and leaves behavior unchanged

mateo-berri accepted the Responses probability gap because #49536 addresses it separately

  • Gemini requests with top_logprobs now send the required logprob settings.

Reviews (5) · Last reviewed commit: "fix(gemini): mark the responseLogprobs s..." · Reviewed by Greptile

@codspeed

codspeed Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 43 untouched benchmarks


Comparing litellm_gemini_top_logprobs (e5a489c) with main (3bd75f1)

Open in CodSpeed

@codecov

codecov Bot commented Oct 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…ob param

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor Author

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 e5a489c. Configure here.

@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@CLAassistant

CLAassistant commented Nov 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This branch has not been deployed

No deployments
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.

2 participants