Skip to content

Conversation

@Chesars
Copy link
Contributor

@Chesars Chesars commented Dec 17, 2025

Relevant issues

Fixes #18137

Pre-Submission checklist

  • I have Added testing in the tests/litellm/ directory
  • 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

Preserve web_fetch_tool_result in multi-turn conversations (same fix as #17746 for web_search).

Example

from litellm import completion

# First turn - Claude fetches a URL
response = completion(
    model="anthropic/claude-sonnet-4-20250514",
    tools=[{"type": "web_fetch_20250910", "name": "web_fetch", "max_uses": 5}],
    messages=[{"role": "user", "content": "Fetch https://example.com and summarize it"}],
)

# Second turn - follow up question (fails)
messages = [
    {"role": "user", "content": "Fetch https://example.com and summarize it"},
    response.choices[0].message.model_dump(),
    {"role": "user", "content": "What was the title?"},
]

response2 = completion(
    model="anthropic/claude-sonnet-4-20250514",
    tools=[{"type": "web_fetch_20250910", "name": "web_fetch", "max_uses": 5}],
    messages=messages,
)

Solution

When Anthropic returns a web_fetch response, it includes both a server_tool_use block and a web_fetch_tool_result block. LiteLLM was only preserving the server_tool_use but dropping the web_fetch_tool_result. This fix captures and stores web_fetch_tool_result in provider_specific_fields so it can be reconstructed in subsequent turns.

Files changed

  • litellm/llms/anthropic/chat/transformation.py - handle web_fetch_tool_result (non-streaming)
  • litellm/llms/anthropic/chat/handler.py - capture web_fetch_tool_result (streaming)
  • tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_handler.py - add 2 unit tests

Tests

Tests Added

  • test_web_fetch_tool_result_captured_in_provider_specific_fields
  • test_web_fetch_tool_result_no_extra_tool_calls
tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_handler.py - 22 passed ✅

…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
@vercel
Copy link

vercel bot commented Dec 17, 2025

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

Project Deployment Review Updated (UTC)
litellm Ready Ready Preview, Comment Dec 17, 2025 6:59pm

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]: web_fetch context breaks multi-turn conversation

1 participant