Skip to content

fix(gemini): use longest-prefix match for context window lookup - #7131

Open
a-yeyang wants to merge 1 commit into
crewAIInc:mainfrom
a-yeyang:fix/gemini-context-window-prefix-match
Open

fix(gemini): use longest-prefix match for context window lookup#7131
a-yeyang wants to merge 1 commit into
crewAIInc:mainfrom
a-yeyang:fix/gemini-context-window-prefix-match

Conversation

@a-yeyang

Copy link
Copy Markdown
Contributor

Summary

  • GeminiCompletion.get_context_window_size() returned the first matching prefix in its lookup table via startswith, so the more specific "gemini-2.0-flash-thinking" entry was always shadowed by the shorter "gemini-2.0-flash" entry listed above it — that row was dead code.
  • Replaced the first-match loop with a longest-matching-prefix selection, so entry order in the table no longer matters and a more specific prefix always wins over a shorter one.

Validation

  • Added test_gemini_context_window_size_prefers_longest_matching_prefix in lib/crewai/tests/llms/google/test_google.py, asserting gemini-2.0-flash-thinking-exp-01-21 resolves to the 32768-token window rather than the 1048576-token gemini-2.0-flash window.
  • Ran the full lib/crewai/tests/llms/google/ suite: 52 passed, 2 skipped (1 pre-existing unrelated failure due to a missing litellm extra in the local dev env, reproduced identically on main).
  • ruff check / ruff format --check pass on both changed files.

Fixes #7129

GeminiCompletion.get_context_window_size() returned the first matching
prefix in the table, so "gemini-2.0-flash-thinking" was always shadowed
by the shorter "gemini-2.0-flash" entry listed above it, making the
thinking-model row dead code.

Fixes crewAIInc#7129
@coderabbitai

coderabbitai Bot commented Aug 27, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 431456b8-0f62-427f-80bf-3007b0248e69

📥 Commits

Reviewing files that changed from the base of the PR and between fcdeb3d and b9450b1.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/llms/providers/gemini/completion.py
  • lib/crewai/tests/llms/google/test_google.py

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The Gemini context window lookup now selects the longest matching model prefix. A regression test verifies the specific thinking model value and the standard flash model value.

Changes

Gemini context window resolution

Layer / File(s) Summary
Longest-prefix lookup and regression coverage
lib/crewai/src/crewai/llms/providers/gemini/completion.py, lib/crewai/tests/llms/google/test_google.py
The lookup selects the longest matching model prefix. Tests verify the expected context windows for thinking and standard flash models.

Suggested reviewers: lucasgomide, lorenzejay

Merge Risk: ⚪ Minimal · up to b9450

The change makes Gemini select the most specific configured context window for a model prefix and adds regression coverage; no actionable merge-blocking risk remains beyond normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: Gemini context window lookup now uses the longest matching prefix.
Description check ✅ Passed The description explains the Gemini prefix-matching bug, the implemented fix, the regression test, and validation results. It is directly related to the changeset.
Linked Issues check ✅ Passed The changes satisfy issue #7129. Gemini models using the "gemini-2.0-flash-thinking" prefix now select the specific entry and return int(32768 * 0.85), while the regression test verifies the behavior.
Out of Scope Changes check ✅ Passed The changes are limited to the Gemini context-window lookup and its regression test. No unrelated code changes are identified.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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] Gemini gemini-2.0-flash-thinking returns the wrong context window (891289 instead of 27852)

1 participant