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 branch 'main' into litellm_add_anthropic_tool_call_results

Sameer Kankuteec3e30a22111
diff --cc litellm/llms/anthropic/chat/handler.py
index fb5fcb3efc5,2ad9a6cdf83..9cc8c24ed13
--- a/litellm/llms/anthropic/chat/handler.py
+++ b/litellm/llms/anthropic/chat/handler.py
@@@ -719,28 -719,32 +719,40 @@@ class ModelResponseIterator
content_block_start=content_block_start,
provider_specific_fields=provider_specific_fields,
)
- elif (
- content_block_start["content_block"]["type"]
- == "web_search_tool_result"
- ):
- # Capture web_search_tool_result for multi-turn reconstruction
- # The full content comes in content_block_start, not in deltas
- # See: https://github.com/BerriAI/litellm/issues/17737
- self.web_search_results.append(
- content_block_start["content_block"]
- )
- provider_specific_fields["web_search_results"] = (
- self.web_search_results
- )
- elif (
- content_block_start["content_block"]["type"]
- == "web_fetch_tool_result"
- ):
- # Capture web_fetch_tool_result for multi-turn reconstruction
- # The full content comes in content_block_start, not in deltas
- # Fixes: https://github.com/BerriAI/litellm/issues/18137
- self.web_search_results.append(
- content_block_start["content_block"]
- )
- provider_specific_fields["web_search_results"] = (
- self.web_search_results
- )
++
+ elif content_block_start["content_block"]["type"].endswith("_tool_result"):
+ # Handle all tool result types (web_search, bash_code_execution, text_editor, etc.)
+ content_type = content_block_start["content_block"]["type"]
+
+ # Special handling for web_search_tool_result for backwards compatibility
+ if content_type == "web_search_tool_result":
+ # Capture web_search_tool_result for multi-turn reconstruction
+ # The full content comes in content_block_start, not in deltas
+ # See: https://github.com/BerriAI/litellm/issues/17737
+ self.web_search_results.append(
+ content_block_start["content_block"]
+ )
+ provider_specific_fields["web_search_results"] = (
+ self.web_search_results
+ )
++ elif content_type == "web_fetch_tool_result":
++ # Capture web_fetch_tool_result for multi-turn reconstruction
++ # The full content comes in content_block_start, not in deltas
++ # Fixes: https://github.com/BerriAI/litellm/issues/18137
++ self.web_search_results.append(
++ content_block_start["content_block"]
++ )
++ provider_specific_fields["web_search_results"] = (
++ self.web_search_results
++ )
+ elif content_type != "tool_search_tool_result":
+ # Handle other tool results (code execution, etc.)
+ # Skip tool_search_tool_result as it's internal metadata
+ if not hasattr(self, "tool_results"):
+ self.tool_results = []
+ self.tool_results.append(content_block_start["content_block"])
+ provider_specific_fields["tool_results"] = self.tool_results
++
elif type_chunk == "content_block_stop":
ContentBlockStop(**chunk) # type: ignore
# check if tool call content block - only for tool_use and server_tool_use blocks
diff --cc litellm/llms/anthropic/chat/transformation.py
index 3a66797c67e,6248f85223c..2bbd6cf17b2
--- a/litellm/llms/anthropic/chat/transformation.py
+++ b/litellm/llms/anthropic/chat/transformation.py
@@@ -1154,21 -1152,22 +1154,27 @@@ class AnthropicConfig(AnthropicModelInf
index=idx,
)
tool_calls.append(tool_call)
- ## TOOL SEARCH TOOL RESULT (skip - this is metadata about tool discovery)
- elif content["type"] == "tool_search_tool_result":
- # This block contains tool_references that were discovered
- # We don't need to include this in the response as it's internal metadata
- pass
- ## WEB SEARCH TOOL RESULT - preserve web search results for multi-turn conversations
- elif content["type"] == "web_search_tool_result":
- if web_search_results is None:
- web_search_results = []
- web_search_results.append(content)
- ## WEB FETCH TOOL RESULT - preserve web fetch results for multi-turn conversations
- ## Fixes: https://github.com/BerriAI/litellm/issues/18137
- elif content["type"] == "web_fetch_tool_result":
- if web_search_results is None:
- web_search_results = []
- web_search_results.append(content)
++
+ ## TOOL RESULTS - handle all tool result types (code execution, etc.)
+ elif content["type"].endswith("_tool_result"):
+ # Skip tool_search_tool_result as it's internal metadata
+ if content["type"] == "tool_search_tool_result":
+ continue
+ # Handle web_search_tool_result separately for backwards compatibility
+ if content["type"] == "web_search_tool_result":
+ if web_search_results is None:
+ web_search_results = []
+ web_search_results.append(content)
++ elif content["type"] == "web_fetch_tool_result":
++ if web_search_results is None:
++ web_search_results = []
++ web_search_results.append(content)
+ else:
+ # All other tool results (bash_code_execution_tool_result, text_editor_code_execution_tool_result, etc.)
+ if tool_results is None:
+ tool_results = []
+ tool_results.append(content)
++
elif content.get("thinking", None) is not None:
if thinking_blocks is None:
thinking_blocks = []