Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions litellm/llms/anthropic/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -417,6 +418,22 @@ 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")
edits: list = []
if isinstance(context_management, dict) and "edits" in context_management:
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))

def get_anthropic_headers(
Expand Down
60 changes: 60 additions & 0 deletions tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1280,3 +1280,63 @@ 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_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

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
Comment on lines +1283 to +1342

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Missing test for OpenAI list format

The three new tests only cover context_management as an Anthropic dict ({"edits": [...]}). The direct Anthropic path (_ensure_context_management_beta_header) also handles the OpenAI list format where context_management is a plain list of edit objects (e.g., [{"type": "compaction", ...}]). A test for this format would catch the gap identified in get_anthropic_beta_list where the list branch is absent.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in dcba26c -- test_openai_list_format_adds_compact_beta covers the list branch.

Loading