Skip to content

fix(gemini): detect Gemini 3+ by major version, not a gemini-3 substring - #37145

Closed
maya-lukas wants to merge 5 commits into
BerriAI:mainfrom
maya-lukas:fix/gemini-rolling-latest-alias-version-detection
Closed

maya-lukas wants to merge 5 commits into
BerriAI:mainfrom
maya-lukas:fix/gemini-rolling-latest-alias-version-detection

Conversation

@maya-lukas

@maya-lukas maya-lukas commented Aug 17, 2026 •

Copy link
Copy Markdown

TLDR

Problem this solves:

  • Gemini 3 detection matched the substring "gemini-3"
  • It misses gemini-flash-latest, which resolves to Gemini 3.x
  • It will also miss gemini-4-* once Google ships it
  • Tool-call replay on those names fails with 400 missing a thought_signature
  • The Flash tier was then re-derived from the same substring, twice, inside the Gemini 3 branch
  • So a rolling alias that reached that branch was demoted within it: reasoning_effort="minimal" returned thinkingLevel="low", and "none" / "disable" likewise

How it solves it:

  • Read the major version off the name, compare against 3
  • Match the three unversioned rolling aliases by exact name
  • Share one _is_gemini_3_flash_or_newer helper for the Flash tier, built on that same version read

User Flow

Before: a developer whose agent calls a tool, then replays the result, gets a 400 on the follow-up turn, so the agent loop dies after its first tool call

  1. They send POST http://localhost:4000/v1/chat/completions with "model": "gemini/gemini-flash-latest", a search_website tool, and one user message
  2. The reply contains a tool call, so they run their tool and collect the result
  3. They send the same route the full history: the user message, the assistant tool call stored under their own id call_abc123, and a role: "tool" result
  4. The response is 400, reading Function call is missing a thought_signature in functionCall parts. This is required for tools to work correctly
  5. Sending the identical history with "model": "gemini/gemini-3.5-flash" returns 200, so the failure looks specific to the alias

After: the same replay returns an answer, so the agent loop continues past its first tool call

  1. They send POST http://localhost:4000/v1/chat/completions with "model": "gemini/gemini-flash-latest", a search_website tool, and one user message
  2. The reply contains a tool call, so they run their tool and collect the result
  3. They send the same route the full history: the user message, the assistant tool call stored under their own id call_abc123, and a role: "tool" result
  4. The response is 200, carrying the assistant's answer summarising the tool result
  5. "model": "gemini/gemini-3.5-flash" still returns 200, exactly as it did before

Relevant issues

Extends the fix for #18245 to the rolling aliases. That issue was closed by 9e3f946, which added the reasoning_effort="minimal" → thinkingLevel="minimal" mapping for Gemini 3 Flash, but keyed the Flash tier on "gemini-3" in model, so it lands for versioned names and not for gemini-flash-latest or gemini-flash-lite-latest. The outer gate and the two Flash-tier checks are the same substring bug, so fixing one without the others moves a rolling alias from "wrong parameter entirely" to "right parameter, one level too high"

Related to #25322, where the reported 400 ... missing a thought_signature comes from the same code path. This PR covers the case where the model's generation is not recognised from its name, and does not change signature propagation from thought parts

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Screenshots / Proof of Fix

Real calls to the Google AI Studio API through a live proxy, no mocks. Both runs use this setup unchanged:

cat > config.yaml <<'YAML'
model_list:
  - model_name: gemini/gemini-flash-latest
    litellm_params:
      model: gemini/gemini-flash-latest
      api_key: os.environ/GEMINI_API_KEY
  - model_name: gemini/gemini-3.5-flash
    litellm_params:
      model: gemini/gemini-3.5-flash
      api_key: os.environ/GEMINI_API_KEY
  - model_name: gemini/gemini-3.7-flash
    litellm_params:
      model: gemini/gemini-3.7-flash
      api_key: os.environ/GEMINI_API_KEY
general_settings:
  master_key: sk-1234
YAML

cat > payload.json <<'JSON'
{
  "model": "gemini/gemini-flash-latest",
  "messages": [
    {"role": "user", "content": "What trips do you have to Iceland? Use the tool."},
    {"role": "assistant", "content": null, "tool_calls": [
      {"id": "call_abc123", "type": "function",
       "function": {"name": "search_website", "arguments": "{\"query\": \"Iceland\"}"}}]},
    {"role": "tool", "tool_call_id": "call_abc123",
     "content": "[{\"title\": \"Iceland Highlands Trek\", \"price\": \"EUR 2400\"}]"}
  ],
  "tools": [{"type": "function", "function": {
    "name": "search_website", "description": "Search the client website",
    "parameters": {"type": "object", "properties": {"query": {"type": "string"}}, "required": ["query"]}}}]
}
JSON

The assistant tool call carries a plain client-side id with no __thought__ signature attached, which is what a client that stores tool calls under its own ids replays. The two explicitly versioned models act as the control: they are already detected correctly, so they must be unaffected

Before (973329e)

the rolling alias

  1. python litellm/proxy/proxy_cli.py --config config.yaml --port 4000
  2. curl -s -w "\nHTTP %{http_code}\n" -X POST http://localhost:4000/v1/chat/completions -H "Authorization: Bearer sk-1234" -H "Content-Type: application/json" -d @payload.json
  3. Output:
HTTP 400
litellm.BadRequestError: GeminiException BadRequestError - {   "error": {     "code": 400,
"message": "Function call is missing a thought_signature in functionCall parts. This is required
for tools to work correctly, and missing thought_signature may lead to degraded model performance.

versioned models, for control

  1. for M in gemini/gemini-3.5-flash gemini/gemini-3.7-flash; do sed "s#gemini/gemini-flash-latest#$M#" payload.json > pm.json; curl -s -o /dev/null -w "$M HTTP %{http_code}\n" -X POST http://localhost:4000/v1/chat/completions -H "Authorization: Bearer sk-1234" -H "Content-Type: application/json" -d @pm.json; done
  2. Output:
gemini/gemini-3.5-flash HTTP 200
gemini/gemini-3.7-flash HTTP 200

After (a686f36)

the rolling alias

  1. python litellm/proxy/proxy_cli.py --config config.yaml --port 4000
  2. curl -s -w "\nHTTP %{http_code}\n" -X POST http://localhost:4000/v1/chat/completions -H "Authorization: Bearer sk-1234" -H "Content-Type: application/json" -d @payload.json
  3. Output:
HTTP 200
answer: We offer the following trip to Iceland:  **Iceland Highlands Trek** - EUR 2,400

versioned models, for control

  1. for M in gemini/gemini-3.5-flash gemini/gemini-3.7-flash; do sed "s#gemini/gemini-flash-latest#$M#" payload.json > pm.json; curl -s -o /dev/null -w "$M HTTP %{http_code}\n" -X POST http://localhost:4000/v1/chat/completions -H "Authorization: Bearer sk-1234" -H "Content-Type: application/json" -d @pm.json; done
  2. Output:
gemini/gemini-3.5-flash HTTP 200
gemini/gemini-3.7-flash HTTP 200

make lint passes at a686f36: circular imports, ruff format, ruff check, ruff strict gate, import safety, basedpyright on tests/e2e, the type-discipline gate and the basedpyright budget gate all report OK, with no budget ceiling needing an update. Lint and tests at the same two commits, for completeness. ruff check on the two touched files reports 66 findings at 973329e and 65 at a686f36, all pre-existing, with none on the changed lines, and ruff format --check is unchanged. pytest tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py goes from 2 failed, 144 passed to 146 passed, and the wider sweep over tests/test_litellm/llms/vertex_ai plus tests/test_litellm/litellm_core_utils/prompt_templates is 1527 passed, 3 skipped at both commits

Flash tier (a190e6b)

_map_reasoning_effort_to_thinking_level and _map_thinking_param each rebuilt the Flash tier with "gemini-3" in model.lower(), so the rolling aliases were demoted inside the branch the earlier commit had just let them into. Both now call _is_gemini_3_flash_or_newer, which reuses the major-version read and so covers Gemini 4 Flash unchanged. Flash-Lite counts, Pro does not, since MINIMAL is a Flash-tier level. Pro behaviour is untouched: minimal stays "low", medium stays "high", and gemini-3.1-pro-preview keeps its medium.

thinkingLevel for reasoning_effort on the rolling alias, before and after:

effort gemini-flash-latest before after gemini-pro-latest
minimal low minimal low, unchanged
none / disable low minimal low, unchanged
medium high medium high, unchanged
low / high unchanged unchanged unchanged

Verification at a190e6b, pinned ruff==0.15.3 per pyproject.toml:

  • pytest tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py is 150 passed, up from 146 at a686f36, the four new tests being the added ones
  • The wider sweep pytest tests/test_litellm/llms/vertex_ai tests/test_litellm/google_genai/test_google_genai_transformation.py is 41 failed, 1305 passed at a190e6b against 41 failed, 1301 passed at a686f36: the same 41 pre-existing environment failures either side, +4 from this commit
  • ruff format --check and ruff check on the changed source file both report clean, matching a686f36 exactly
  • The new tests add 17 E712 findings to the test file, the same rule and the same == True style as the 28 already there, including the ones added earlier in this PR
  • basedpyright reports 803 errors at a190e6b and 803 at a686f36 on the changed source file, so the budget ceiling does not move

The live-API Before/After above was run at a686f36 and is not re-run for this commit: it exercises the thought-signature replay, which this commit does not touch. This commit changes only which thinkingLevel is selected. The full make lint was likewise not re-run here; the gates this commit could plausibly move are ruff format, ruff check and the basedpyright budget, all three reported above against the same baseline.

The diff stays minimal on purpose: the local is_gemini3flash name is left as it was at both call sites rather than renamed to match the helper, and the test file takes pure additions with no edits to existing tests.

Type

🐛 Bug Fix

Caveats (if any)

  • Alias set covers only names with no version at all
  • Google adding a fourth tier alias would need a one-line addition
  • Also re-enables id forwarding and v1alpha for these aliases
  • One substring bug, three call sites: the generation gate plus the two Flash-tier derivations. Happy to split the Flash-tier commit out if you would rather review it separately, it stands alone on top of the first

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

`_is_gemini_3_or_newer` matched the substring "gemini-3", so Google's
rolling aliases (`gemini-flash-latest`, `gemini-flash-lite-latest` and
`gemini-pro-latest`) were not recognised even though they resolve to
Gemini 3.x. Every Gemini 3 behaviour keyed off that check was therefore
silently disabled for them, including the documented
`skip_thought_signature_validator` fallback.

The visible symptom is a multi-turn tool call failing with
`400 Function call is missing a thought_signature in functionCall parts`
whenever the assistant turn's tool call does not carry LiteLLM's
`__thought__`-encoded id, for example history replayed by a client that
uses its own tool-call ids.

Matching is by exact alias name rather than an `endswith("-latest")`
check, because versioned names such as
`gemini-2.5-flash-native-audio-latest` also end in `-latest` and are not
Gemini 3.
@CLAassistant

CLAassistant commented Aug 17, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@greptile-apps

greptile-apps Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR replaces Gemini 3 substring detection with numeric major-version parsing while explicitly handling unversioned rolling aliases.

  • Reuses the generation check for Flash-tier thinking-level selection.
  • Adds regression coverage for rolling aliases, future major versions, thought signatures, and reasoning-effort mappings.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py Introduces shared major-version and Flash-tier detection and applies it consistently to Gemini request transformation.
tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py Adds focused regression coverage for rolling aliases, later Gemini generations, thought-signature fallback, and thinking-level mapping.

Reviews (3): Last reviewed commit: "fix(gemini): read the Flash tier off the..." | Re-trigger Greptile

@codecov

codecov Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Addresses review feedback: the module constant now carries the `Final`
annotation required by LIT010, and the explanatory comments are folded
into the existing docstring so the source keeps to the repository's
comment convention.
@maya-lukas
maya-lukas changed the base branch from main to litellm_internal_staging August 17, 2026 08:22
The substring check would have reported Gemini 4 and later as pre-3,
reintroducing the same class of bug the moment Google ships an explicitly
versioned `gemini-4-*` model. Parsing the major version off the name and
comparing against 3 makes every later release work without a code change,
and it also excludes `gemini-2.5-flash-native-audio-latest` on version
rather than by special-casing the name.

The alias set now covers only names that carry no version at all.
@maya-lukas maya-lukas changed the title fix(gemini): treat rolling -latest aliases as Gemini 3+ fix(gemini): detect Gemini 3+ by major version, not a gemini-3 substring Aug 17, 2026
@codspeed

codspeed Bot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing maya-lukas:fix/gemini-rolling-latest-alias-version-detection (aad8f45) with litellm_internal_staging (02522a5)1

Open in CodSpeed

Footnotes

  1. No successful run was found on litellm_internal_staging (168a005) during the generation of this report, so 02522a5 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@maya-lukas
maya-lukas force-pushed the fix/gemini-rolling-latest-alias-version-detection branch from 39cb416 to a686f36 Compare August 17, 2026 09:42
@maya-lukas

Copy link
Copy Markdown
Author

@greptileai

`_map_reasoning_effort_to_thinking_level` and `_map_thinking_param` each
re-derived the Flash tier with the same substring the outer gate just
stopped using, so a rolling alias reached the Gemini 3 branch and was then
demoted inside it: `reasoning_effort="minimal"` on `gemini-flash-latest`
returned `thinkingLevel="low"`, and `"none"` / `"disable"` likewise, so the
caller silently got more thinking than it asked for. Closes BerriAI#18245.

Both sites now share `_is_gemini_3_flash_or_newer`, which reuses the major
version read and so covers Gemini 4 Flash without a code change. Flash-Lite
counts; Pro does not, since MINIMAL is a Flash-tier level. Pro keeps "low"
for minimal and "high" for medium, unchanged.
@maya-lukas
maya-lukas force-pushed the fix/gemini-rolling-latest-alias-version-detection branch from 71ef3d6 to a190e6b Compare August 31, 2026 10:32
@maya-lukas

Copy link
Copy Markdown
Author

@greptileai

@maya-lukas
maya-lukas requested a review from a team September 7, 2026 14:36
@yuneng-berri
yuneng-berri deleted the branch BerriAI:main September 13, 2026 04:42
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
@devin-ai-integration
devin-ai-integration Bot changed the base branch from litellm_internal_staging to main September 23, 2026 15:05
@mateo-berri

Copy link
Copy Markdown
Contributor

Superseded by #42465, which already treats gemini-flash-latest, gemini-flash-lite-latest, and gemini-pro-latest as Gemini 3 or newer on main. Closing, thanks for the report

@mateo-berri mateo-berri closed this Oct 1, 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.

4 participants