From b6a5c19ed93948ce69e46065391863afc29faf46 Mon Sep 17 00:00:00 2001 From: Jonathan Wrede Date: Sun, 10 May 2026 15:36:42 +0000 Subject: [PATCH] fix(anthropic): auto-inject compact beta header for context_management get_anthropic_beta_list() did not detect context_management in optional_params, so Bedrock InvokeModel requests with compaction never received the required "compact-2026-01-12" in anthropic_beta. Bedrock rejects context_management without the beta header with "Extra inputs are not permitted". The direct Anthropic path already handled this via _ensure_context_management_beta_header (HTTP headers), but the Bedrock path builds its beta list from get_anthropic_beta_list() which feeds the request body's anthropic_beta array. Fixes #27532 --- litellm/llms/anthropic/common_utils.py | 15 ++++++ .../anthropic/test_anthropic_common_utils.py | 48 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index 869a7c5fbc4..944b43123d7 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -14,6 +14,7 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import ( from litellm.llms.base_llm.base_utils import BaseLLMModelInfo, BaseTokenCounter from litellm.llms.base_llm.chat.transformation import BaseLLMException from litellm.types.llms.anthropic import ( + ANTHROPIC_BETA_HEADER_VALUES, ANTHROPIC_HOSTED_TOOLS, ANTHROPIC_OAUTH_BETA_HEADER, ANTHROPIC_OAUTH_TOKEN_PREFIX, @@ -417,6 +418,20 @@ class AnthropicModelInfo(BaseLLMModelInfo): if mcp_server_used: betas.append("mcp-client-2025-04-04") + if optional_params: + context_management = optional_params.get("context_management") + if isinstance(context_management, dict) and "edits" in context_management: + for edit in context_management.get("edits", []): + edit_type = edit.get("type", "") + if edit_type in ("compact_20260112", "compaction"): + betas.append( + ANTHROPIC_BETA_HEADER_VALUES.COMPACT_2026_01_12.value + ) + else: + betas.append( + ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_MANAGEMENT_2025_06_27.value + ) + return list(set(betas)) def get_anthropic_headers( diff --git a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py index 2f57ce5d180..edf49b0b0b8 100644 --- a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py +++ b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py @@ -1280,3 +1280,51 @@ class TestAnthropicThinkingSignatureSelfHeal: config.transform_anthropic_messages_request_on_http_error(err, data) assert "thinking" not in data assert data["messages"] == [] + + +class TestGetAnthropicBetaListContextManagement: + """ + Regression tests for https://github.com/BerriAI/litellm/issues/27532 + get_anthropic_beta_list must include compact/context-management betas + when context_management is in optional_params, so Bedrock InvokeModel + receives the required anthropic_beta field. + """ + + def test_compact_edit_adds_compact_beta(self): + from litellm.llms.anthropic.common_utils import AnthropicModelInfo + + info = AnthropicModelInfo() + betas = info.get_anthropic_beta_list( + model="claude-sonnet-4-6", + optional_params={ + "context_management": { + "edits": [{"type": "compact_20260112"}], + } + }, + ) + assert "compact-2026-01-12" in betas + + def test_non_compact_edit_adds_context_management_beta(self): + from litellm.llms.anthropic.common_utils import AnthropicModelInfo + + info = AnthropicModelInfo() + betas = info.get_anthropic_beta_list( + model="claude-sonnet-4-6", + optional_params={ + "context_management": { + "edits": [{"type": "summarize"}], + } + }, + ) + assert "context-management-2025-06-27" in betas + + def test_no_context_management_no_extra_betas(self): + from litellm.llms.anthropic.common_utils import AnthropicModelInfo + + info = AnthropicModelInfo() + betas = info.get_anthropic_beta_list( + model="claude-sonnet-4-6", + optional_params={"max_tokens": 100}, + ) + assert "compact-2026-01-12" not in betas + assert "context-management-2025-06-27" not in betas