Skip to content

fix(gemini): place longest model prefix first in context_windows table (fix #7129) - #7146

Open
Jaymr1031-Z wants to merge 3 commits into
crewAIInc:mainfrom
Jaymr1031-Z:fix/gemini-thinking-context-window
Open

fix(gemini): place longest model prefix first in context_windows table (fix #7129)#7146
Jaymr1031-Z wants to merge 3 commits into
crewAIInc:mainfrom
Jaymr1031-Z:fix/gemini-thinking-context-window

Conversation

@Jaymr1031-Z

Copy link
Copy Markdown

Fixes #7129

Problem

In GeminiCompletion.get_context_window_size(), context_windows had gemini-2.0-flash listed before gemini-2.0-flash-thinking. Since the loop breaks and returns on the first prefix match, gemini-2.0-flash-thinking was unreachable dead code, causing it to return 891289 instead of 27852.

Solution

  • Reordered context_windows dictionary keys in GeminiCompletion to place longest model prefixes first (e.g., gemini-2.0-flash-thinking and gemini-2.0-flash-lite before gemini-2.0-flash), matching the design in OpenAI/Azure providers.
  • Added test est_gemini_thinking_context_window in est_llm.py.

@coderabbitai

coderabbitai Bot commented Aug 28, 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: 473ee7ae-c35e-43d7-9e5c-fac1bddd87fe

📥 Commits

Reviewing files that changed from the base of the PR and between 80ca7fd and 7e135b2.

📒 Files selected for processing (1)
  • lib/crewai/src/crewai/llms/providers/gemini/completion.py

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


📝 Walkthrough

Walkthrough

The Gemini provider now selects context windows by longest model-name prefix. Synchronous and asynchronous calls now handle hook-blocking exceptions by emitting call-denied events and re-raising them. Tests cover distinct limits for Gemini thinking and standard Flash models.

Changes

Gemini completion behavior

Layer / File(s) Summary
Context window prefix matching and regression coverage
lib/crewai/src/crewai/llms/providers/gemini/completion.py, lib/crewai/tests/test_llm.py
The lookup sorts model prefixes by descending key length. Tests verify 32,768 tokens for Gemini thinking models and 1,048,576 tokens for standard Gemini Flash models.
Synchronous and asynchronous hook blocking
lib/crewai/src/crewai/llms/providers/gemini/completion.py
The call and acall paths pass formatted messages to the before-call hook, emit call-denied events for HookAborted and LLMCallBlockedError, and re-raise the exceptions.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant GeminiCompletion
  participant BeforeCallHook
  participant CallDeniedEvent
  Caller->>GeminiCompletion: call or acall with formatted messages
  GeminiCompletion->>BeforeCallHook: invoke before-LLM-call hook
  BeforeCallHook-->>GeminiCompletion: raise HookAborted or LLMCallBlockedError
  GeminiCompletion->>CallDeniedEvent: emit call-denied event
  GeminiCompletion-->>Caller: re-raise blocking exception
Loading

Suggested reviewers: lorenzejay

Merge Risk: ⚪ Minimal · up to 7e135

The PR fixes Gemini model context-window selection by prioritizing the most specific model prefix and adds regression coverage. No actionable merge-blocking risk remains beyond normal checks and review.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The Gemini context-window changes are in scope, but the updates to call and acall change before-LLM-call hook error handling by catching HookAborted and LLMCallBlockedError, emitting a call-denied eve… Remove the unrelated hook-handling changes from this pull request, or link an issue that defines those requirements and explains why they belong in the same scope.
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the Gemini context-window fix and the longest-prefix matching change. It is specific to the main change and references issue #7129.
Description check ✅ Passed The description explains the prefix-matching bug, the expected and actual values, the implemented solution, and the regression test. It is directly related to the changeset.
Linked Issues check ✅ Passed The changes satisfy issue #7129. Gemini context-window prefixes are matched longest-first, so gemini-2.0-flash-thinking-* resolves to the specific 32,768-token entry and returns 27,852 after the usage…
Full details: Linked Issues check

Explanation

The changes satisfy issue #7129. Gemini context-window prefixes are matched longest-first, so gemini-2.0-flash-thinking-* resolves to the specific 32,768-token entry and returns 27,852 after the usage ratio. A regression test covers the fix.

Full details: Out of Scope Changes check

Explanation

The Gemini context-window changes are in scope, but the updates to call and acall change before-LLM-call hook error handling by catching HookAborted and LLMCallBlockedError, emitting a call-denied event, and re-raising the exception. These changes are not covered by issue #7129 or the stated objectives.

  • Fix all pre-merge checks with AI
✨ 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