diff --git a/src/google/adk/models/anthropic_llm.py b/src/google/adk/models/anthropic_llm.py index 3edbaeb3be0..54d2066ce05 100644 --- a/src/google/adk/models/anthropic_llm.py +++ b/src/google/adk/models/anthropic_llm.py @@ -330,12 +330,17 @@ def to_claude_role(role: Optional[str]) -> Literal["user", "assistant"]: # Mapping of Anthropic stop_reason strings to FinishReason enum values -_STOP_REASON_MAPPING: dict[anthropic_types.StopReason, types.FinishReason] = { +# Keyed by str: "model_context_window_exceeded" is newer than the minimum +# supported anthropic SDK's StopReason literal. +_STOP_REASON_MAPPING: dict[str, types.FinishReason] = { "end_turn": types.FinishReason.STOP, "stop_sequence": types.FinishReason.STOP, "tool_use": types.FinishReason.STOP, "pause_turn": types.FinishReason.STOP, "max_tokens": types.FinishReason.MAX_TOKENS, + # Generation stopped because the context window filled up, so the output + # is truncated just as with max_tokens. + "model_context_window_exceeded": types.FinishReason.MAX_TOKENS, "refusal": types.FinishReason.SAFETY, } diff --git a/tests/unittests/models/test_anthropic_llm.py b/tests/unittests/models/test_anthropic_llm.py index b7a54248f33..f57e7bcd689 100644 --- a/tests/unittests/models/test_anthropic_llm.py +++ b/tests/unittests/models/test_anthropic_llm.py @@ -4243,6 +4243,7 @@ async def mock_coro(): ("stop_sequence", types.FinishReason.STOP), ("tool_use", types.FinishReason.STOP), ("max_tokens", types.FinishReason.MAX_TOKENS), + ("model_context_window_exceeded", types.FinishReason.MAX_TOKENS), ("pause_turn", types.FinishReason.STOP), ("refusal", types.FinishReason.SAFETY), (None, None),