fix(anthropic): send the computer-use beta named after each computer tool version

This commit is contained in:
mateo-berri 2026-10-29 08:30:37 -07:00
parent b25ea48c4b
commit c19c5c8e69
3 changed files with 108 additions and 34 deletions

View file

@ -113,6 +113,7 @@ _BEDROCK_VERSION_SUFFIX_RE: Final = re.compile(r"-v\d+(?::\d+)?$")
_INFERENCE_PROFILE_MINOR_RE: Final = re.compile(r":\d+$")
_DATED_RELEASE_SUFFIX_RE: Final = re.compile(r"-\d{8}$")
_DOTTED_VERSION_RE: Final = re.compile(r"(\d)\.(\d)")
_COMPUTER_TOOL_VERSION_RE: Final = re.compile(r"computer_(\d{4})(\d{2})(\d{2})")
_CLAUDE_CODE_BILLING_HEADER_PREFIX: Final = "x-anthropic-billing-header:"
OBJECT_MAPPING_ADAPTER: Final = TypeAdapter(dict[object, object])
OBJECT_LIST_ADAPTER: Final = TypeAdapter(list[object])
@ -1165,24 +1166,13 @@ class AnthropicModelInfo(BaseLLMModelInfo):
return None
return anthropic_beta_header.split(",")
def get_computer_tool_beta_header(self, computer_tool_version: str) -> str:
"""
Get the appropriate beta header for a given computer tool version.
Args:
computer_tool_version: The computer tool version (e.g., 'computer_20250124', 'computer_20241022')
Returns:
The corresponding beta header string
"""
computer_tool_beta_mapping: Final = {
"computer_20250124": "computer-use-2025-01-24",
"computer_20241022": "computer-use-2024-10-22",
}
return computer_tool_beta_mapping.get(
computer_tool_version,
"computer-use-2024-10-22", # Default fallback
)
def get_computer_tool_beta_header(self, computer_tool_version: str) -> str | None:
"""Anthropic names each computer tool's beta after its date: computer_20251124 needs computer-use-2025-11-24."""
version: Final = _COMPUTER_TOOL_VERSION_RE.fullmatch(computer_tool_version)
if version is None:
return None
year, month, day = version.groups()
return f"computer-use-{year}-{month}-{day}"
def get_anthropic_beta_list(
self,
@ -1214,9 +1204,9 @@ class AnthropicModelInfo(BaseLLMModelInfo):
if effort_used:
betas.append(ANTHROPIC_EFFORT_BETA_HEADER) # effort-2025-11-24
if computer_tool_used:
beta_header: Final = self.get_computer_tool_beta_header(computer_tool_used)
betas.append(beta_header)
computer_beta: Final = self.get_computer_tool_beta_header(computer_tool_used) if computer_tool_used else None
if computer_beta is not None:
betas.append(computer_beta)
# Anthropic no longer requires the prompt-caching beta header
# Prompt caching now works automatically when cache_control is used in messages
@ -1305,9 +1295,9 @@ class AnthropicModelInfo(BaseLLMModelInfo):
# Anthropic no longer requires the prompt-caching beta header
# Prompt caching now works automatically when cache_control is used in messages
# Reference: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
if computer_tool_used:
beta_header: Final = self.get_computer_tool_beta_header(computer_tool_used)
betas.add(beta_header)
computer_beta: Final = self.get_computer_tool_beta_header(computer_tool_used) if computer_tool_used else None
if computer_beta is not None:
betas.add(computer_beta)
# if pdf_used:
# betas.add("pdfs-2024-09-25")
if file_id_used:

View file

@ -7477,23 +7477,22 @@ def test_process_anthropic_headers_with_no_matching_headers():
@pytest.mark.parametrize(
"computer_tool_used, prompt_caching_set, expected_beta_header",
[
(True, False, True),
(False, True, False),
(True, True, True),
(False, False, False),
("computer_20250124", False, "computer-use-2025-01-24"),
(None, True, None),
("computer_20251124", True, "computer-use-2025-11-24"),
(None, False, None),
],
)
def test_anthropic_beta_header(computer_tool_used, prompt_caching_set, expected_beta_header):
headers = litellm.AnthropicConfig().get_anthropic_headers(
def test_anthropic_beta_header(
computer_tool_used: str | None, prompt_caching_set: bool, expected_beta_header: str | None
) -> None:
headers: Final = litellm.AnthropicConfig().get_anthropic_headers(
api_key="fake-api-key",
computer_tool_used=computer_tool_used,
prompt_caching_set=prompt_caching_set,
)
if expected_beta_header:
assert "anthropic-beta" in headers
else:
assert "anthropic-beta" not in headers
assert headers.get("anthropic-beta") == expected_beta_header
@pytest.mark.parametrize(

View file

@ -4559,3 +4559,88 @@ def test_shared_legacy_thinking_translation_drops_a_non_numeric_budget_under_dro
)
def test_is_claude_tool_target(custom_llm_provider: str, model: str, expected: bool):
assert is_claude_tool_target(custom_llm_provider=custom_llm_provider, model=model) is expected
COMPUTER_20251124_TOOL: Final = {
"type": "computer_20251124",
"function": {"name": "computer", "parameters": {"display_width_px": 1024, "display_height_px": 768}},
}
@pytest.mark.parametrize(
("computer_tool_version", "expected_beta"),
[
("computer_20241022", "computer-use-2024-10-22"),
("computer_20250124", "computer-use-2025-01-24"),
("computer_20251124", "computer-use-2025-11-24"),
("computer_20991231", "computer-use-2099-12-31"),
("computer_use_preview", None),
],
)
def test_computer_tool_beta_is_named_after_the_tool_version(computer_tool_version: str, expected_beta: str | None):
from litellm.llms.anthropic.common_utils import AnthropicModelInfo
betas: Final = AnthropicModelInfo().get_anthropic_beta_list(
model="claude-opus-4-5", computer_tool_used=computer_tool_version, custom_llm_provider="anthropic"
)
assert [beta for beta in betas if beta.startswith("computer-use-")] == ([expected_beta] if expected_beta else [])
def test_anthropic_chat_sends_the_computer_use_2025_11_24_beta_for_computer_20251124(
respx_mock: respx.MockRouter, local_beta_headers_config: None
):
route: Final = respx_mock.post("https://api.anthropic.com/v1/messages").mock(
return_value=httpx.Response(
200,
json={
"id": "msg_computer",
"type": "message",
"role": "assistant",
"content": [
{"type": "tool_use", "id": "toolu_1", "name": "computer", "input": {"action": "screenshot"}}
],
"model": "claude-opus-4-5-20251101",
"stop_reason": "tool_use",
"stop_sequence": None,
"usage": {"input_tokens": 12, "output_tokens": 2},
},
)
)
litellm.completion(
model="anthropic/claude-opus-4-5",
api_key="mock_api_key",
messages=[{"role": "user", "content": "Take a screenshot of the screen."}],
tools=[COMPUTER_20251124_TOOL],
)
sent_betas: Final = route.calls.last.request.headers["anthropic-beta"].split(",")
assert [beta for beta in sent_betas if beta.startswith("computer-use-")] == ["computer-use-2025-11-24"]
def test_vertex_ai_chat_sends_the_computer_use_2025_11_24_beta_for_computer_20251124(
local_beta_headers_config: None,
):
from litellm.anthropic_beta_headers_manager import update_request_with_filtered_beta
from litellm.llms.vertex_ai.vertex_ai_partner_models.anthropic.transformation import VertexAIAnthropicConfig
config: Final = VertexAIAnthropicConfig()
messages: Final = [{"role": "user", "content": "Take a screenshot of the screen."}]
optional_params: Final = config.map_openai_params(
non_default_params={"tools": [COMPUTER_20251124_TOOL]},
optional_params={"max_tokens": 100, "is_vertex_request": True},
model="claude-opus-4-5",
drop_params=False,
)
headers: Final[dict[str, str]] = {}
request_data: Final = config.transform_request(
model="claude-opus-4-5", messages=messages, optional_params=optional_params, litellm_params={}, headers=headers
)
filtered_headers, _ = update_request_with_filtered_beta(
headers=headers, request_data=request_data, provider="vertex_ai"
)
sent_betas: Final = filtered_headers["anthropic-beta"].split(",")
assert [beta for beta in sent_betas if beta.startswith("computer-use-")] == ["computer-use-2025-11-24"]