From 3cc452a54cd7b6860428fb4b222fbb4829140c70 Mon Sep 17 00:00:00 2001 From: kaiisfree Date: Sun, 5 Apr 2026 07:34:44 +0000 Subject: [PATCH] Fix: map Anthropic `compaction` finish_reason to `stop` instead of `length` When Anthropic's context compaction triggers with pause_after_compaction=True, the API returns finish_reason "compaction". This was incorrectly mapped to "length", which signals output truncation due to max_tokens and causes downstream consumers to retry or attempt continuation. Compaction means the context was summarized and the model paused naturally, so "stop" is the semantically correct mapping. Fixes #25165 Co-Authored-By: Claude Opus 4.6 --- litellm/litellm_core_utils/core_helpers.py | 2 +- tests/test_litellm/litellm_core_utils/test_core_helpers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index 22006be21af..85e2b970cfb 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -65,7 +65,7 @@ _FINISH_REASON_MAP: dict[str, OpenAIChatCompletionFinishReason] = { "max_tokens": "length", "tool_use": "tool_calls", "refusal": "content_filter", - "compaction": "length", + "compaction": "stop", # Cohere "COMPLETE": "stop", "ERROR_TOXIC": "content_filter", diff --git a/tests/test_litellm/litellm_core_utils/test_core_helpers.py b/tests/test_litellm/litellm_core_utils/test_core_helpers.py index 8397fc22242..8f0c6d418c5 100644 --- a/tests/test_litellm/litellm_core_utils/test_core_helpers.py +++ b/tests/test_litellm/litellm_core_utils/test_core_helpers.py @@ -66,7 +66,7 @@ class TestMapFinishReasonAnthropic: ("end_turn", "stop"), ("max_tokens", "length"), ("tool_use", "tool_calls"), - ("compaction", "length"), + ("compaction", "stop"), ("content_filtered", "content_filter"), ], )