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
This commit is contained in:
Jonathan Wrede 2026-05-10 15:36:42 +00:00
parent 0af33fbe70
commit b6a5c19ed9
2 changed files with 63 additions and 0 deletions

View file

@ -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(

View file

@ -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