fix(anthropic): capture web_search_tool_result in streaming for multi-turn conversations #17798
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Title
fix(anthropic): capture web_search_tool_result in streaming for multi-turn conversations
Relevant issues
Fixes #17737 for streaming with Anthropic web search tool
Pre-Submission checklist
tests/litellm/directory, Adding at least 1 test is a hard requirementmake test-unitType
🐛 Bug Fix
Changes
This fix addresses two issues with Anthropic web search streaming:
1. Fix trailing
{}in tool call argumentsweb_search_tool_resultblocks haveinput_json_deltathat were emitted as tool calls, with invalid JSON like:Fix: Added
current_content_block_typetracking to only emit tool calls fortool_useandserver_tool_useblocks.2. Capture
web_search_tool_resultin streamingRequest:
Anthropic sends (streaming SSE):
Before fix:
web_search_tool_resultwas ignored →provider_specific_fields.web_search_resultswas empty → multi-turn failedAfter fix: LiteLLM returns:
{ "choices": [{ "delta": { "tool_calls": [{"id": "srvtoolu_01ABC", "function": {"name": "web_search", "arguments": "{...}"}}], "provider_specific_fields": { "web_search_results": [{"type": "web_search_tool_result", "tool_use_id": "srvtoolu_01ABC", "content": [...]}] } } }] }Now multi-turn works because
stream_chunk_builderpreservesprovider_specific_fields.Tests added
Files changed
litellm/llms/anthropic/chat/handler.py- Streaming handler fixeslitellm/main.py-stream_chunk_builderprovider_specific_fields handlingtests/test_litellm/llms/anthropic/chat/test_anthropic_chat_handler.py- 3 new tests