Skip to content

Conversation

@Chesars
Copy link
Contributor

@Chesars Chesars commented Dec 11, 2025

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

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🐛 Bug Fix

Changes

This fix addresses two issues with Anthropic web search streaming:

1. Fix trailing {} in tool call arguments

web_search_tool_result blocks have input_json_delta that were emitted as tool calls, with invalid JSON like:

arguments = '{"query": "fun otter facts"}{}{}{}'  ❌

Fix: Added current_content_block_type tracking to only emit tool calls for tool_use and server_tool_use blocks.

2. Capture web_search_tool_result in streaming

Request:

response = completion(
    model="anthropic/claude-sonnet-4-20250514",
    messages=[{"role": "user", "content": "Search the web for weather in Tokyo"}],
    stream=True,
    tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 1}]
)

Anthropic sends (streaming SSE):

event: content_block_start
data: {"type": "content_block_start", "content_block": {"type": "server_tool_use", "id": "srvtoolu_01ABC", "name": "web_search"}}

event: content_block_start  
data: {"type": "content_block_start", "content_block": {"type": "web_search_tool_result", "tool_use_id": "srvtoolu_01ABC", "content": [{"url": "...", "title": "...", "encrypted_content": "..."}]}}

Before fix: web_search_tool_result was ignored → provider_specific_fields.web_search_results was empty → multi-turn failed

After 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_builder preserves provider_specific_fields .

Tests added

Files changed

  • litellm/llms/anthropic/chat/handler.py - Streaming handler fixes
  • litellm/main.py - stream_chunk_builder provider_specific_fields handling
  • tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_handler.py - 3 new tests

…-turn conversations

This fix addresses two issues with Anthropic web search streaming:

1. Fix trailing {} in tool call arguments
   - web_search_tool_result blocks have input_json_delta events that were
     incorrectly emitted as tool calls
   - Added current_content_block_type tracking to only emit tool calls for
     tool_use and server_tool_use blocks

2. Capture web_search_tool_result for multi-turn
   - The web_search_tool_result content comes ALL AT ONCE in content_block_start
   - Now captured in provider_specific_fields.web_search_results
   - stream_chunk_builder combines these for final message
   - Allows multi-turn conversations to work with streaming web search
@vercel
Copy link

vercel bot commented Dec 11, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
litellm Error Error Dec 11, 2025 0:28am

@krrishdholakia krrishdholakia merged commit 2e303bf into BerriAI:main Dec 11, 2025
4 of 7 checks passed
@Chesars Chesars deleted the fix/anthropic-streaming-web-search-extra-braces branch December 11, 2025 16:20
Chesars added a commit to Chesars/litellm that referenced this pull request Dec 17, 2025
…tions

Fixes BerriAI#18137

Similar to the fix for web_search_tool_result (BerriAI#17746, BerriAI#17798), this PR
preserves web_fetch_tool_result blocks in multi-turn conversations.

Changes:
- Add handling for web_fetch_tool_result in transformation.py (non-streaming)
- Add capture of web_fetch_tool_result in handler.py (streaming)
- Fix streaming tool arguments bug where empty input {} was prepended to
  actual arguments by using empty string instead of str({})
- Add unit tests for web_fetch_tool_result handling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Multi-turn conversations with Anthropic web search + custom tools broken - server_tool_use incorrectly converted to tool_use

2 participants