From bbc265d912cf5f6518f9fa4c9d3f08d8d050295d Mon Sep 17 00:00:00 2001 From: JingHao-Leon <102573344+JingHao-Leon@users.noreply.github.com> Date: Fri, 25 Sep 2026 10:12:04 +0800 Subject: [PATCH] fix(core_helpers): map Anthropic model_context_window_exceeded stop reason to length MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Anthropic returns stop_reason="model_context_window_exceeded" when generation stops because the context window filled up. _FINISH_REASON_MAP had no entry for it, so map_finish_reason logged a warning and returned "stop" — clients cannot distinguish a truncated response from a complete one, on both chat completions and the Responses API. Map it to "length" (consistent with max_tokens and the existing compaction entry) and cover it in the Anthropic parametrized finish-reason tests. Fixes #43012 --- litellm/litellm_core_utils/core_helpers.py | 1 + tests/unit/litellm_core_utils/test_core_helpers.py | 1 + 2 files changed, 2 insertions(+) diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index b095b4b12c6..5afaad61ec8 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -199,6 +199,7 @@ _FINISH_REASON_MAP: Final[dict[str, OpenAIChatCompletionFinishReason]] = { "stop_sequence": "stop", "end_turn": "stop", "max_tokens": "length", + "model_context_window_exceeded": "length", "tool_use": "tool_calls", "refusal": "content_filter", "compaction": "length", diff --git a/tests/unit/litellm_core_utils/test_core_helpers.py b/tests/unit/litellm_core_utils/test_core_helpers.py index 2a6dd347d5f..ed8a090f064 100644 --- a/tests/unit/litellm_core_utils/test_core_helpers.py +++ b/tests/unit/litellm_core_utils/test_core_helpers.py @@ -178,6 +178,7 @@ class TestMapFinishReasonAnthropic: ("stop_sequence", "stop"), ("end_turn", "stop"), ("max_tokens", "length"), + ("model_context_window_exceeded", "length"), ("tool_use", "tool_calls"), ("compaction", "length"), ("content_filtered", "content_filter"),