Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_lit6103_tool_reference_passthrough

mateo-berrie26ea0bc95d1
diff --cc tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
index 9c88d7a67bb,95fbd06547b..27cabc1e724
--- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
+++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
@@@ -3999,70 -3999,96 +3999,165 @@@ def test_translate_anthropic_messages_t
]
+def _tool_reference_block(tool_name="WebFetch"):
+ return {"type": "tool_reference", "tool_name": tool_name}
+
+
+def test_tool_result_tool_reference_is_carried_through_untouched():
+ adapter = LiteLLMAnthropicMessagesAdapter()
+
+ result = adapter.translate_anthropic_messages_to_openai(
+ messages=[
+ _anthropic_tool_use_turn("toolu_01"),
+ _anthropic_tool_result_turn({"toolu_01": [_tool_reference_block()]}),
+ ]
+ )
+
+ assert [m["role"] for m in result] == ["assistant", "tool"]
+ assert result[1]["tool_call_id"] == "toolu_01"
+ assert result[1]["content"] == [{"type": "tool_reference", "tool_name": "WebFetch"}]
+
+
+def test_tool_result_text_beside_tool_reference_keeps_both_parts_in_order():
+ adapter = LiteLLMAnthropicMessagesAdapter()
+
+ result = adapter.translate_anthropic_messages_to_openai(
+ messages=[
+ _anthropic_tool_use_turn("toolu_01"),
+ _anthropic_tool_result_turn(
+ {"toolu_01": [{"type": "text", "text": "loaded"}, _tool_reference_block("Grep")]}
+ ),
+ ]
+ )
+
+ assert result[1]["content"] == [
+ {"type": "text", "text": "loaded"},
+ {"type": "tool_reference", "tool_name": "Grep"},
+ ]
+
+
+ "tool_result_content",
+ [
+ [],
+ None,
+ "",
+ {"not": "a list"},
+ [{"type": "future_block", "payload": 1}],
+ [{"type": "search_result", "source": "https://example.com", "title": "t", "content": []}],
+ ],
+ ids=["empty_list", "null", "empty_string", "non_list", "unknown_block", "search_result_only"],
+)
+def test_tool_result_without_translatable_content_still_answers_its_tool_use(tool_result_content):
+ adapter = LiteLLMAnthropicMessagesAdapter()
+
+ result = adapter.translate_anthropic_messages_to_openai(
+ messages=[
+ _anthropic_tool_use_turn("toolu_01"),
+ {
+ "role": "user",
+ "content": [{"type": "tool_result", "tool_use_id": "toolu_01", "content": tool_result_content}],
+ },
+ ]
+ )
+
+ assert result == [
+ result[0],
+ {"role": "tool", "tool_call_id": "toolu_01", "content": ""},
+ ]
+ assert result[0]["role"] == "assistant"
++
++
+ def _openai_response_with_usage(usage: Usage) -> ModelResponse:
+ return ModelResponse(
+ id="resp_web_search",
+ model="gemini-3-flash-preview",
+ choices=[
+ Choices(
+ finish_reason="stop",
+ index=0,
+ message=Message(role="assistant", content="searched"),
+ )
+ ],
+ usage=usage,
+ )
+
+
+ def test_translate_openai_response_to_anthropic_maps_gemini_web_search_usage():
+ from litellm.types.utils import PromptTokensDetailsWrapper
+
+ usage = Usage(
+ prompt_tokens=385,
+ completion_tokens=566,
+ total_tokens=951,
+ prompt_tokens_details=PromptTokensDetailsWrapper(text_tokens=385, web_search_requests=2),
+ )
+
+ anthropic_response = LiteLLMAnthropicMessagesAdapter().translate_openai_response_to_anthropic(
+ response=_openai_response_with_usage(usage)
+ )
+
+ assert anthropic_response["usage"]["server_tool_use"] == {"web_search_requests": 2}
+
+
+ def test_translate_openai_response_to_anthropic_maps_server_tool_use_web_search_usage():
+ from litellm.types.utils import ServerToolUse
+
+ usage = Usage(
+ prompt_tokens=100,
+ completion_tokens=40,
+ total_tokens=140,
+ server_tool_use=ServerToolUse(web_search_requests=3),
+ )
+
+ anthropic_response = LiteLLMAnthropicMessagesAdapter().translate_openai_response_to_anthropic(
+ response=_openai_response_with_usage(usage)
+ )
+
+ assert anthropic_response["usage"]["server_tool_use"] == {"web_search_requests": 3}
+
+
+ def test_translate_openai_response_to_anthropic_omits_server_tool_use_without_web_search():
+ usage = Usage(prompt_tokens=100, completion_tokens=40, total_tokens=140)
+
+ anthropic_response = LiteLLMAnthropicMessagesAdapter().translate_openai_response_to_anthropic(
+ response=_openai_response_with_usage(usage)
+ )
+
+ assert "server_tool_use" not in anthropic_response["usage"]
+
+
+ def test_completion_cost_on_translated_anthropic_response_includes_web_search():
+ from litellm.types.utils import PromptTokensDetailsWrapper
+
+ adapter = LiteLLMAnthropicMessagesAdapter()
+ with_search = adapter.translate_openai_response_to_anthropic(
+ response=_openai_response_with_usage(
+ Usage(
+ prompt_tokens=385,
+ completion_tokens=566,
+ total_tokens=951,
+ prompt_tokens_details=PromptTokensDetailsWrapper(text_tokens=385, web_search_requests=2),
+ )
+ )
+ )
+ without_search = adapter.translate_openai_response_to_anthropic(
+ response=_openai_response_with_usage(Usage(prompt_tokens=385, completion_tokens=566, total_tokens=951))
+ )
+
+ cost_with_search = litellm.completion_cost(
+ completion_response=with_search,
+ model="gemini/gemini-3-flash-preview",
+ call_type="anthropic_messages",
+ )
+ cost_without_search = litellm.completion_cost(
+ completion_response=without_search,
+ model="gemini/gemini-3-flash-preview",
+ call_type="anthropic_messages",
+ )
+
+ per_query_cost = litellm.model_cost["gemini/gemini-3-flash-preview"]["search_context_cost_per_query"][
+ "search_context_size_medium"
+ ]
+ assert per_query_cost > 0
+ assert cost_with_search - cost_without_search == pytest.approx(2 * per_query_cost)