Skip to content

fix(anthropic): preserve web_fetch_tool_result in multi-turn conversations#18142

Merged
2 commits merged into
BerriAI:mainfrom
Chesars:fix/web-fetch-multi-turn-18137
Jan 12, 2026
Merged

fix(anthropic): preserve web_fetch_tool_result in multi-turn conversations#18142
2 commits merged into
BerriAI:mainfrom
Chesars:fix/web-fetch-multi-turn-18137

Conversation

@Chesars

@Chesars Chesars commented Dec 17, 2025

Copy link
Copy Markdown
Contributor

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

vercel Bot commented Dec 17, 2025

Copy link
Copy Markdown

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

Project Deployment Review Updated (UTC)
litellm Ready Ready Preview, Comment Dec 26, 2025 10:54pm

@Sameerlite

Copy link
Copy Markdown
Collaborator

@Chesars Can you fix the conflict

Resolve conflict in handler.py arguments initialization:
- Use upstream's simpler arguments="" approach
- Both versions fix the same bug (preventing '{}' prepending)
- Upstream solution is cleaner and already tested

Preserves PR's unique contributions:
- web_fetch_tool_result support in handler.py (streaming)
- web_fetch_tool_result support in transformation.py (non-streaming)
- Tests for web_fetch_tool_result functionality
@Chesars

Chesars commented Dec 26, 2025

Copy link
Copy Markdown
Contributor Author

@Chesars Can you fix the conflict

@Sameerlite Fixed 👍

@ghost ghost merged commit 9a8e781 into BerriAI:main Jan 12, 2026
6 of 7 checks passed
@Chesars Chesars deleted the fix/web-fetch-multi-turn-18137 branch January 12, 2026 11:58
This pull request was closed.
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

2 participants