From cf6c277386295090a411f0ea6040d71b390dddef Mon Sep 17 00:00:00 2001 From: 0xxmemo Date: Mon, 6 Apr 2026 15:56:14 -0500 Subject: [PATCH] Add support for server_tool_use and web_search_tool_result in FakeAnthropicMessagesStreamIterator - Implemented handling for "server_tool_use" and "web_search_tool_result" block types in the FakeAnthropicMessagesStreamIterator. - Added logic to emit content_block_start and content_block_stop events for both block types, aligning with Anthropic's native streaming format. - Enhanced the iterator's functionality to support new message types, improving integration with external tools. --- .../messages/fake_stream_iterator.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/fake_stream_iterator.py b/litellm/llms/anthropic/experimental_pass_through/messages/fake_stream_iterator.py index 7fc9b00f2c7..9dde357f4ee 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/fake_stream_iterator.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/fake_stream_iterator.py @@ -169,6 +169,42 @@ class FakeAnthropicMessagesStreamIterator: block_dict = cast(Dict[str, Any], block) chunks.extend(self._create_content_block_chunks(block_dict, index)) + elif block_type == "server_tool_use": + # Emit the full block as content_block_start (same as + # Anthropic's native streaming format). + content_block_start = { + "type": "content_block_start", + "index": index, + "content_block": block_dict, + } + chunks.append( + f"event: content_block_start\ndata: {json.dumps(content_block_start)}\n\n".encode() + ) + + # content_block_stop + content_block_stop = {"type": "content_block_stop", "index": index} + chunks.append( + f"event: content_block_stop\ndata: {json.dumps(content_block_stop)}\n\n".encode() + ) + + elif block_type == "web_search_tool_result": + # Emit the full block as content_block_start (same as + # Anthropic's native streaming format). + content_block_start = { + "type": "content_block_start", + "index": index, + "content_block": block_dict, + } + chunks.append( + f"event: content_block_start\ndata: {json.dumps(content_block_start)}\n\n".encode() + ) + + # content_block_stop + content_block_stop = {"type": "content_block_stop", "index": index} + chunks.append( + f"event: content_block_stop\ndata: {json.dumps(content_block_stop)}\n\n".encode() + ) + # 5. message_delta event (with final usage and stop_reason) # Include cache usage fields so clients that only read message_delta # (like Claude Code's SDK) see the full input token breakdown.