From 69e5741e5bbb2e3ca278ec9a5c1daa1dfef97f18 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 22:25:27 -0700 Subject: [PATCH] fix(anthropic): accept server_tool_use and web_search_tool_result blocks when costing a /v1/messages response The short-circuit now returns native web search blocks, and the cost path validated the served body against a content union that only allowed text, tool_use and thinking blocks, so non-streaming requests answered 500 --- litellm/types/llms/anthropic.py | 19 ++++++++++++++++ .../test_websearch_short_circuit.py | 22 ++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/litellm/types/llms/anthropic.py b/litellm/types/llms/anthropic.py index de83e6ea66e..1a1312dca9a 100644 --- a/litellm/types/llms/anthropic.py +++ b/litellm/types/llms/anthropic.py @@ -713,6 +713,23 @@ class AnthropicResponseContentBlockRedactedThinking(LiteLLMBaseModel): data: str +class AnthropicResponseContentBlockServerToolUse(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True) + + type: Literal["server_tool_use"] + id: str + name: str + input: Mapping[str, object] + + +class AnthropicResponseContentBlockWebSearchToolResult(LiteLLMBaseModel): + model_config = ConfigDict(frozen=True) + + type: Literal["web_search_tool_result"] + tool_use_id: str + content: Sequence[Mapping[str, object]] | Mapping[str, object] + + class AnthropicResponseUsageBlock(LiteLLMBaseModel): model_config = ConfigDict(extra="allow") @@ -744,6 +761,8 @@ class AnthropicResponse(LiteLLMBaseModel): | AnthropicResponseContentBlockToolUse | AnthropicResponseContentBlockThinking | AnthropicResponseContentBlockRedactedThinking + | AnthropicResponseContentBlockServerToolUse + | AnthropicResponseContentBlockWebSearchToolResult ] """Content generated by the model.""" diff --git a/tests/unit/integrations/websearch_interception/test_websearch_short_circuit.py b/tests/unit/integrations/websearch_interception/test_websearch_short_circuit.py index a29781d9370..4bda9f95c6d 100644 --- a/tests/unit/integrations/websearch_interception/test_websearch_short_circuit.py +++ b/tests/unit/integrations/websearch_interception/test_websearch_short_circuit.py @@ -6,6 +6,7 @@ and executes the search directly without routing through the backend LLM. """ import json +import time from unittest.mock import AsyncMock, patch import pytest @@ -457,7 +458,7 @@ class TestShortCircuitThroughMessagesEntryPoint: NATIVE_TOOL = {"type": "web_search_20250305", "name": "web_search"} - async def _send(self, *, tools: list[dict], stream: bool): + async def _send(self, *, tools: list[dict], stream: bool, **kwargs): import litellm logger = WebSearchInterceptionLogger(enabled_providers=["bedrock"]) @@ -470,6 +471,7 @@ class TestShortCircuitThroughMessagesEntryPoint: tools=tools, max_tokens=64, stream=stream, + **kwargs, ) if not stream: return response @@ -489,6 +491,24 @@ class TestShortCircuitThroughMessagesEntryPoint: assert server_tool_use["input"] == {"query": "latest python release"} assert tool_result["tool_use_id"] == server_tool_use["id"] + @pytest.mark.asyncio + async def test_native_blocks_reply_can_be_costed(self): + from litellm.litellm_core_utils.litellm_logging import Logging + + logging_obj = Logging( + model="bedrock/converse/us.anthropic.claude-sonnet-5-5", + messages=[{"role": "user", "content": "latest python release"}], + stream=False, + call_type="anthropic_messages", + start_time=time.time(), + litellm_call_id="short-circuit-cost", + function_id="short-circuit-cost", + ) + logging_obj.update_environment_variables(litellm_params={}, optional_params={}) + response = await self._send(tools=[self.NATIVE_TOOL], stream=False, litellm_logging_obj=logging_obj) + + assert logging_obj.response_cost_calculator(result=response) == 0.0 + @pytest.mark.asyncio async def test_native_tool_streams_native_blocks(self): body = await self._send(tools=[self.NATIVE_TOOL], stream=True)