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
This commit is contained in:
mateo-berri 2026-10-28 22:25:27 -07:00
parent b859137b24
commit 69e5741e5b
2 changed files with 40 additions and 1 deletions

View file

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

View file

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