Repository navigation
fix(models): map Anthropic model_context_window_exceeded to MAX_TOKENS - #7584
Open
pcbeingused333 wants to merge 1 commit into
Open
pcbeingused333 wants to merge 1 commit into
pcbeingused333 wants to merge 1 commit into
Conversation
Claude Sonnet 4.5 and newer end a turn with stop_reason "model_context_window_exceeded" when generation fills the context window. It was missing from the stop reason mapping and fell through to FINISH_REASON_UNSPECIFIED, so a truncated response looked like one without a reported finish reason, and callers that check for MAX_TOKENS (e.g. the LLM classifier) accepted the truncated output.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
pcbeingused333
force-pushed
the
fix/anthropic-context-window-finish-reason
branch
from
October 17, 2026 03:26
222a6f2 to
c297707
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
2. Or, if no issue exists, describe the change:
Problem:
Claude Sonnet 4.5 and newer models end a turn with
stop_reason: "model_context_window_exceeded"when generation stops because the context window is full.AnthropicLlm's_STOP_REASON_MAPPINGdoesn't list it, soto_google_genai_finish_reasonmaps it toFINISH_REASON_UNSPECIFIED, in both the streaming and non-streaming paths.The output is truncated just as with
max_tokens, but nothing downstream sees that:is_reported_finish_reasontreatsUNSPECIFIEDas "no finish reason reported";MAX_TOKENS, such as the LLM classifier's "ran out of output tokens" guard, accept the truncated text as a complete answer.Solution:
Map
model_context_window_exceededtoFinishReason.MAX_TOKENS, the same asmax_tokens. The mapping is now keyed bystr, because the stop reason is newer than theStopReasonliteral in the minimum supportedanthropic(0.78). Unknown values still map toFINISH_REASON_UNSPECIFIED.Testing Plan
Unit Tests:
Added a
model_context_window_exceededcase totest_to_google_genai_finish_reason.pytest tests/unittests/models/test_anthropic_llm.py: 184 passed (anthropic 1.15.0). Onmainonly the new case fails.pyink --checkandisort --check-onlyare clean on the touched files, andmypyreports the same errors onanthropic_llm.pyas onmain.Manual End-to-End (E2E) Tests:
Not run: triggering this stop reason needs a Claude request that fills the context window. The change only affects the stop reason → finish reason mapping, which the unit test exercises directly.
Checklist
Additional context
This change was drafted with Claude Code; the tests above were run before opening the PR.
🤖 Generated with Claude Code