From b6a5c19ed93948ce69e46065391863afc29faf46 Mon Sep 17 00:00:00 2001 From: Jonathan Wrede Date: Sun, 10 May 2026 15:36:42 +0000 Subject: [PATCH 1/3] 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 869a7c5fbc42..944b43123d7e 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -14,6 +14,7 @@ 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 @@ def get_anthropic_beta_list( 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 2f57ce5d1806..edf49b0b0b82 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 @@ def test_anthropic_messages_config_http_retry_helpers(self): 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 From dcba26c8aa99fdf02e7b146318340a9d5d789c58 Mon Sep 17 00:00:00 2001 From: Jonathan Wrede Date: Sun, 10 May 2026 18:18:29 +0000 Subject: [PATCH 2/3] handle OpenAI list format for context_management beta detection The direct Anthropic path supports context_management as either a dict with an "edits" key or a plain list of edit objects. Mirror that logic in get_anthropic_beta_list so Bedrock/Vertex paths also inject the correct beta headers when the OpenAI list format is used. --- litellm/llms/anthropic/common_utils.py | 22 ++++++++++--------- .../anthropic/test_anthropic_common_utils.py | 12 ++++++++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index 944b43123d7e..0803b76ea52d 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -420,17 +420,19 @@ def get_anthropic_beta_list( if optional_params: context_management = optional_params.get("context_management") + edits: list = [] 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 - ) + edits = context_management.get("edits", []) + elif isinstance(context_management, list): + edits = context_management + for edit in 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)) 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 edf49b0b0b82..d9c1f13afa0c 100644 --- a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py +++ b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py @@ -1318,6 +1318,18 @@ def test_non_compact_edit_adds_context_management_beta(self): ) assert "context-management-2025-06-27" in betas + def test_openai_list_format_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": [{"type": "compaction"}], + }, + ) + assert "compact-2026-01-12" in betas + def test_no_context_management_no_extra_betas(self): from litellm.llms.anthropic.common_utils import AnthropicModelInfo From 9e85c8bf146f5005afdd4f1a8c585656da81cddc Mon Sep 17 00:00:00 2001 From: Jonathan Wrede Date: Tue, 12 May 2026 18:22:43 +0000 Subject: [PATCH 3/3] compact beta takes precedence over context-management beta When mixed edit types are present (e.g. summarize + compact), only emit the compact beta header. Previously both betas were appended independently per edit. --- litellm/llms/anthropic/common_utils.py | 23 ++++++++++++------- .../anthropic/test_anthropic_common_utils.py | 18 +++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index 0803b76ea52d..f341ac0cbfc1 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -425,14 +425,21 @@ def get_anthropic_beta_list( edits = context_management.get("edits", []) elif isinstance(context_management, list): edits = context_management - for edit in 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 - ) + has_compact = any( + edit.get("type", "") in ("compact_20260112", "compaction") + for edit in edits + ) + has_other_edits = any( + edit.get("type", "") + and edit.get("type", "") not in ("compact_20260112", "compaction") + for edit in edits + ) + if has_compact: + betas.append(ANTHROPIC_BETA_HEADER_VALUES.COMPACT_2026_01_12.value) + elif has_other_edits: + betas.append( + ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_MANAGEMENT_2025_06_27.value + ) return list(set(betas)) 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 d9c1f13afa0c..d3883b08a06c 100644 --- a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py +++ b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py @@ -1340,3 +1340,21 @@ def test_no_context_management_no_extra_betas(self): ) assert "compact-2026-01-12" not in betas assert "context-management-2025-06-27" not in betas + + def test_mixed_edits_uses_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": "summarize"}, + {"type": "compact_20260112"}, + ], + } + }, + ) + assert "compact-2026-01-12" in betas + assert "context-management-2025-06-27" not in betas