Skip to content

fix(websearch_interception): emit native web_search blocks on the short-circuit path - #32196

Closed
rkage wants to merge 1 commit into
BerriAI:mainfrom
rkage:fix/websearch-shortcircuit-native-blocks
Closed

rkage wants to merge 1 commit into
BerriAI:mainfrom
rkage:fix/websearch-shortcircuit-native-blocks

Conversation

@rkage

@rkage rkage commented Jul 5, 2026 •

Copy link
Copy Markdown

Relevant issues

Fixes #31902

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Screenshots / Proof of Fix

End-to-end against a local litellm proxy → GitHub Copilot backend → Tavily search (real API calls, no mocks). Same streaming web_search sub-request Claude Code issues, before vs. after:

Before (unpatched main / 1.91.0rc1) — response stream is text-only:

$ curl -sN -X POST 'http://localhost:4200/v1/messages?beta=true' \
    -H 'Authorization: Bearer …' -H 'anthropic-version: 2023-06-01' \
    -d '{"model":"claude-opus-4-8","stream":true,"max_tokens":1024,
         "tools":[{"type":"web_search_20250305","name":"web_search"}],
         "messages":[{"role":"user","content":"Perform a web search for the query: newest Kubernetes minor version release 2026"}]}'

content_block types: ["text"]          # ← no server_tool_use, no web_search_tool_result
→ Claude Code reports: "Did 0 searches", discards results, falls back to WebFetch

After (this PR) — native blocks present, sources carried:

content_block types: ["server_tool_use", "web_search_tool_result", "text"]
  server_tool_use.input.query = "…newest Kubernetes minor version release 2026"
  web_search_tool_result.content = 5 sources (url + title each)
SSE: 3 content_block_start / 3 content_block_stop, balanced, valid JSON
→ Claude Code reports: "Did 1 search"

Type

🐛 Bug Fix

Changes

  • litellm/integrations/websearch_interception/handler.py — emit server_tool_use + web_search_tool_result + text unconditionally on the short-circuit path; advertise usage.server_tool_use.web_search_requests = 1.
  • litellm/llms/anthropic/experimental_pass_through/messages/fake_stream_iterator.py — serialize the server_tool_use and web_search_tool_result block types (previously dropped → malformed SSE under stream=true).
  • Tests — updated test_litellm_standard_tool_short_circuit_* to the correct post-conversion contract; added regression tests for the post-conversion shape, structured-source passthrough, and streaming serialization.

@CLAassistant

CLAassistant commented Jul 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@greptile-apps

greptile-apps Bot commented Jul 5, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes a bug where the websearch short-circuit path returned text-only responses, causing Claude Code to report "Did 0 searches". The root cause was that the pre-request hook had already renamed web_search_20250305 → litellm_web_search before try_short_circuit_search ran, so the native-tool detection always missed and native blocks were never emitted.

  • handler.py: Drops the conditional native-tool detection in try_short_circuit_search and unconditionally emits server_tool_use + web_search_tool_result + text blocks, also advertising usage.server_tool_use.web_search_requests = 1.
  • fake_stream_iterator.py: Adds SSE serialization for the two new block types so stream=True requests produce well-formed content_block_start/content_block_stop pairs for each block.
  • Tests: Updates the previously-incorrect text-only assertion and adds regression tests covering the post-conversion tool shape, structured source passthrough, and streaming serialization.

Confidence Score: 4/5

Safe to merge; the fix correctly addresses the tool-renaming ordering issue and existing tests are strengthened rather than weakened.

The logic change is well-reasoned and the new tests validate the corrected contract end-to-end. One gap: the server_tool_use.web_search_requests usage field added to the synthetic non-streaming response is silently dropped by the streaming serializer's message_delta builder, leaving an inconsistency between the two paths that could affect usage-aware clients.

fake_stream_iterator.py — the delta_usage construction does not propagate the server_tool_use usage entry.

Important Files Changed

Filename Overview
litellm/integrations/websearch_interception/handler.py Unconditionally emits server_tool_use + web_search_tool_result + text on the short-circuit path; correctly addresses the root cause (pre-request hook had already renamed the tool before detection ran).
litellm/llms/anthropic/experimental_pass_through/messages/fake_stream_iterator.py Adds serialization for server_tool_use and web_search_tool_result block types; server_tool_use usage is not forwarded in message_delta/message_start events.
tests/test_litellm/integrations/websearch_interception/test_websearch_native_blocks.py Updated the standard-tool short-circuit test to assert native blocks (valid fix); the old text-only assertion was testing the buggy behavior, not a feature, so the change is correct.
tests/test_litellm/integrations/websearch_interception/test_websearch_short_circuit.py Adds regression tests for post-conversion tool shape, structured source passthrough, and streaming SSE serialization; all tests use mocks and are free of real network calls.

Comments Outside Diff (1)

  1. litellm/llms/anthropic/experimental_pass_through/messages/fake_stream_iterator.py, line 190-208 (link)

    P2 server_tool_use usage not forwarded in message_delta

    The PR adds usage.server_tool_use.web_search_requests = 1 to the synthetic response to advertise the search count, but _create_streaming_chunks only copies input_tokens, output_tokens, cache_creation_input_tokens, and cache_read_input_tokens into delta_usage. Any client that reads the message_delta SSE event's usage (the standard streaming consumption path) will see no server_tool_use entry. The streaming test also uses a response dict without server_tool_use in usage, so this gap is not covered. Adding if usage.get("server_tool_use"): delta_usage["server_tool_use"] = usage["server_tool_use"] here would make the streaming and non-streaming paths consistent.

Reviews (1): Last reviewed commit: "fix(websearch_interception): emit native..." | Re-trigger Greptile

@codecov

codecov Bot commented Jul 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…ircuit path

The short-circuit path (`try_short_circuit_search`) returned a text-only
synthetic response for every real request, because it gated native-block
emission on `is_anthropic_native_web_search_tool(tool)`. That check can never
be true at this point: the pre-request hook rewrites every native
`web_search_*` tool to `litellm_web_search` (type=None) *before* the
short-circuit runs, so the native branch was dead code.

Consequences for clients that consume the standalone /v1/messages search
sub-request (Claude Code, Claude Desktop, Cowork):
  - No `server_tool_use` block → Claude Code reports "Did 0 searches" and
    discards the results, falling back to WebFetch.
  - No `web_search_tool_result` block → citation panels stay empty.

Fix:
  - handler.py: emit `server_tool_use` + `web_search_tool_result` + `text`
    unconditionally on the short-circuit path. Reaching this code means every
    tool is a web-search tool for a provider that can't run search natively,
    which is exactly the short-circuit contract. Also advertise
    `usage.server_tool_use.web_search_requests = 1`.
  - fake_stream_iterator.py: teach the streaming serializer the
    `server_tool_use` and `web_search_tool_result` block types. Claude Code
    issues the sub-request with stream=True; previously these blocks were
    dropped (bare content_block_stop with no matching start → malformed SSE).

Tests:
  - Update `test_litellm_standard_tool_short_circuit_*` to assert the
    post-conversion (`litellm_web_search`) shape emits native blocks — this is
    the only shape production ever takes, not a "non-native" case.
  - Add regression tests for the post-conversion tool shape, structured-source
    passthrough, and streaming serialization of both block types.

Fixes BerriAI#31902
@rkage
rkage force-pushed the fix/websearch-shortcircuit-native-blocks branch from 59d46d9 to 14693c0 Compare July 5, 2026 17:40
@yuneng-berri
yuneng-berri deleted the branch BerriAI:main September 13, 2026 04:28
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
@devin-ai-integration
devin-ai-integration Bot changed the base branch from litellm_internal_staging to main September 23, 2026 15:51
@devin-ai-integration
devin-ai-integration Bot requested a review from a team September 23, 2026 15:51
@mateo-berri

Copy link
Copy Markdown
Contributor

#40967 fixes the same gap but only for native web_search tools, so other callers keep text-only replies. We plan to merge that one

@rkage rkage closed this Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants