fix(websearch): search only the query inside Claude Code's web search prompt

Match Claude Code's exact "Perform a web search for the query:" prefix instead of a broad regex family, keep any quotes the user typed so exact-phrase searches still work, and test the search request at the HTTP boundary.
This commit is contained in:
mateo-berri 2026-10-28 13:50:27 -07:00
parent 40ef1e120f
commit 028a0e5a8b
2 changed files with 78 additions and 137 deletions

View file

@ -8,7 +8,6 @@ server-side using litellm router's search tools.
import asyncio
import math
import re
import uuid
from collections.abc import AsyncIterator, Mapping, Sequence
from dataclasses import dataclass
@ -98,10 +97,7 @@ WEBSEARCH_NATIVE_BLOCKS_METADATA_KEY: Final = "websearch_native_blocks"
_RESPONSE_CONTENT_FIELD: Final = "content"
_CLAUDE_CODE_SEARCH_PREFIX_RE: Final = re.compile(
r"^\s*(?:Perform\s+a\s+)?(?:web\s+)?search\s+for\s+(?:the\s+)?query:\s*",
re.IGNORECASE,
)
_CLAUDE_CODE_WEB_SEARCH_PROMPT_PREFIX: Final = "Perform a web search for the query:"
_ResponseT: Final = TypeVar("_ResponseT")
@ -299,27 +295,6 @@ class WebSearchInterceptionLogger(CustomLogger):
"""
return validated_max_agentic_loops(max_agentic_loops, field="websearch_interception_params.max_agentic_loops")
@classmethod
def _extract_short_circuit_query(cls, raw_message: str) -> str:
"""Extract the intended search terms from a short-circuit user message.
Clients such as Claude Code format standalone WebSearch requests with an
instructional wrapper (e.g. 'Perform a web search for the query: <query>').
Strip known instructional prefixes so the search backend receives only
the intended search terms rather than query wrapper keywords.
"""
prefix_match: Final[re.Match[str] | None] = _CLAUDE_CODE_SEARCH_PREFIX_RE.match(raw_message)
stripped_prefix: Final[str] = (
raw_message[prefix_match.end() :].strip() if prefix_match is not None else raw_message.strip()
)
if prefix_match is not None and (
(stripped_prefix.startswith('"') and stripped_prefix.endswith('"'))
or (stripped_prefix.startswith("'") and stripped_prefix.endswith("'"))
):
return stripped_prefix[1:-1].strip()
return stripped_prefix if (stripped_prefix or prefix_match is not None) else raw_message.strip()
async def try_short_circuit_search(
self,
model: str,
@ -390,11 +365,8 @@ class WebSearchInterceptionLogger(CustomLogger):
get_last_user_message,
)
raw_query: Final = get_last_user_message(cast(list[AllMessageValues], messages))
if not raw_query:
return None
query: Final = self._extract_short_circuit_query(raw_query)
last_user_message: Final = get_last_user_message(cast(list[AllMessageValues], messages)) or ""
query: Final = last_user_message.removeprefix(_CLAUDE_CODE_WEB_SEARCH_PROMPT_PREFIX).strip()
if not query:
return None

View file

@ -6,10 +6,13 @@ and executes the search directly without routing through the backend LLM.
"""
import json
from typing import Final
from unittest.mock import AsyncMock, patch
import pytest
import respx
import litellm
from litellm.integrations.websearch_interception.handler import (
WebSearchInterceptionLogger,
)
@ -35,7 +38,9 @@ class TestTryShortCircuitSearch:
"""Single web_search_20250305 tool → short-circuit fires"""
logger = WebSearchInterceptionLogger(enabled_providers=[provider])
with patch.object(logger, "_execute_search", new_callable=AsyncMock) as mock_search:
with patch.object(
logger, "_execute_search", new_callable=AsyncMock
) as mock_search:
mock_search.return_value = (
"Title: Result\nURL: https://example.com\nSnippet: test",
None,
@ -120,7 +125,9 @@ class TestTryShortCircuitSearch:
result = await logger.try_short_circuit_search(
model="github_copilot/claude-sonnet-5.5",
messages=[{"role": "user", "content": "Search for something"}],
tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 8}],
tools=[
{"type": "web_search_20250305", "name": "web_search", "max_uses": 8}
],
custom_llm_provider="github_copilot",
)
@ -134,12 +141,16 @@ class TestTryShortCircuitSearch:
use the agentic loop which includes a follow-up LLM synthesis step.
The short-circuit must not fire for them.
"""
logger = WebSearchInterceptionLogger(enabled_providers=["bedrock", "github_copilot"])
logger = WebSearchInterceptionLogger(
enabled_providers=["bedrock", "github_copilot"]
)
result = await logger.try_short_circuit_search(
model="bedrock/us.anthropic.claude-sonnet-4-5-20250929-v1:0",
messages=[{"role": "user", "content": "Search for something"}],
tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 8}],
tools=[
{"type": "web_search_20250305", "name": "web_search", "max_uses": 8}
],
custom_llm_provider="bedrock",
)
@ -153,7 +164,9 @@ class TestTryShortCircuitSearch:
result = await logger.try_short_circuit_search(
model="github_copilot/claude-sonnet-5.5",
messages=[],
tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 8}],
tools=[
{"type": "web_search_20250305", "name": "web_search", "max_uses": 8}
],
custom_llm_provider="github_copilot",
)
@ -165,7 +178,6 @@ class TestTryShortCircuitSearch:
[
" ",
"Perform a web search for the query: ",
'Perform a web search for the query: ""',
],
)
async def test_does_not_short_circuit_empty_or_whitespace_user_message(self, empty_prompt: str):
@ -186,13 +198,17 @@ class TestTryShortCircuitSearch:
"""Search failure → response with error message, not exception"""
logger = WebSearchInterceptionLogger(enabled_providers=["github_copilot"])
with patch.object(logger, "_execute_search", new_callable=AsyncMock) as mock_search:
with patch.object(
logger, "_execute_search", new_callable=AsyncMock
) as mock_search:
mock_search.side_effect = RuntimeError("Tavily API error")
result = await logger.try_short_circuit_search(
model="github_copilot/claude-sonnet-5.5",
messages=[{"role": "user", "content": "Search for something"}],
tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 8}],
tools=[
{"type": "web_search_20250305", "name": "web_search", "max_uses": 8}
],
custom_llm_provider="github_copilot",
)
@ -205,7 +221,9 @@ class TestTryShortCircuitSearch:
"""Synthetic response has all required AnthropicMessagesResponse fields"""
logger = WebSearchInterceptionLogger(enabled_providers=["github_copilot"])
with patch.object(logger, "_execute_search", new_callable=AsyncMock) as mock_search:
with patch.object(
logger, "_execute_search", new_callable=AsyncMock
) as mock_search:
mock_search.return_value = ("search results here", None)
result = await logger.try_short_circuit_search(
@ -232,65 +250,45 @@ class TestTryShortCircuitSearch:
# Query extraction tests
# ---------------------------------------------------------------------------
PERPLEXITY_SEARCH_URL: Final = "https://api.perplexity.ai/search"
class TestQueryExtraction:
"""Direct unit tests for query extraction without application test doubles."""
@pytest.mark.parametrize(
"raw_prompt,expected_query",
[
(
"Perform a web search for the query: LiteLLM latest version release",
"LiteLLM latest version release",
),
(
'Perform a web search for the query: "who is the maintainer of litellm"',
"who is the maintainer of litellm",
),
(
" perform a web search for the query: fastapi SSE streaming ",
"fastapi SSE streaming",
),
(
"Search for the query: python 3.13 changelog",
"python 3.13 changelog",
),
(
"web search for query: litellm",
"litellm",
),
(
"Search for Claude Code releases",
"Search for Claude Code releases",
),
(
'"LiteLLM latest version release"',
'"LiteLLM latest version release"',
),
(
"'fastapi SSE streaming'",
"'fastapi SSE streaming'",
),
(
"Perform a web search for the query: ",
"",
),
(
'Perform a web search for the query: ""',
"",
),
(
"",
"",
),
],
@pytest.fixture
def perplexity_search(monkeypatch: pytest.MonkeyPatch, respx_mock: respx.MockRouter) -> respx.Route:
monkeypatch.setenv("PERPLEXITYAI_API_KEY", "test-perplexity-key")
monkeypatch.delenv("PERPLEXITY_API_BASE", raising=False)
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
litellm.in_memory_llm_clients_cache.flush_cache()
return respx_mock.post(PERPLEXITY_SEARCH_URL).respond(
json={"results": [{"title": "Releases", "url": "https://example.com/releases", "snippet": "Release notes"}]}
)
def test_extract_short_circuit_query_strips_claude_code_prefix(self, raw_prompt: str, expected_query: str):
"""Claude Code wraps standalone searches in 'Perform a web search for the query: ...'.
_extract_short_circuit_query must extract only the actual query terms and preserve
exact-phrase quotes when no wrapper was present, without application doubles.
"""
assert WebSearchInterceptionLogger._extract_short_circuit_query(raw_prompt) == expected_query
@pytest.mark.asyncio
@pytest.mark.parametrize(
("user_message", "searched_query"),
[
("Perform a web search for the query: litellm latest release", "litellm latest release"),
('Perform a web search for the query: "litellm proxy"', '"litellm proxy"'),
],
)
async def test_short_circuit_searches_the_query_inside_claude_codes_prompt(
perplexity_search: respx.Route, user_message: str, searched_query: str
):
logger: Final = WebSearchInterceptionLogger(enabled_providers=["bedrock"])
result: Final = await logger.try_short_circuit_search(
model="bedrock/converse/us.anthropic.claude-sonnet-4-5-20250929-v1:0",
messages=[{"role": "user", "content": user_message}],
tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 8}],
custom_llm_provider="bedrock",
)
assert perplexity_search.call_count == 1
assert json.loads(perplexity_search.calls.last.request.content)["query"] == searched_query
assert result is not None
server_tool_use: Final = next(block for block in result["content"] if block["type"] == "server_tool_use")
assert server_tool_use["input"]["query"] == searched_query
# ---------------------------------------------------------------------------
@ -326,7 +324,9 @@ class TestShortCircuitEntryPoint:
)
logger = WebSearchInterceptionLogger(enabled_providers=["github_copilot"])
with patch.object(logger, "_execute_search", new_callable=AsyncMock) as mock_search:
with patch.object(
logger, "_execute_search", new_callable=AsyncMock
) as mock_search:
mock_search.return_value = ("results", None)
with patch("litellm.callbacks", [logger]):
result = await _try_websearch_short_circuit(
@ -352,7 +352,9 @@ class TestShortCircuitEntryPoint:
)
logger = WebSearchInterceptionLogger(enabled_providers=["github_copilot"])
with patch.object(logger, "_execute_search", new_callable=AsyncMock) as mock_search:
with patch.object(
logger, "_execute_search", new_callable=AsyncMock
) as mock_search:
mock_search.return_value = ("streaming results", None)
with patch("litellm.callbacks", [logger]):
result = await _try_websearch_short_circuit(
@ -415,7 +417,9 @@ class TestShortCircuitEntryPoint:
)
logger = WebSearchInterceptionLogger(enabled_providers=["github_copilot"])
with patch.object(logger, "_execute_search", new_callable=AsyncMock) as mock_search:
with patch.object(
logger, "_execute_search", new_callable=AsyncMock
) as mock_search:
mock_search.return_value = ("streaming results", None)
with patch("litellm.callbacks", [logger]):
# Simulate what anthropic_messages() does: original_stream=True
@ -443,7 +447,9 @@ class TestShortCircuitEntryPoint:
)
logger = WebSearchInterceptionLogger(enabled_providers=["github_copilot"])
with patch.object(logger, "_execute_search", new_callable=AsyncMock) as mock_search:
with patch.object(
logger, "_execute_search", new_callable=AsyncMock
) as mock_search:
mock_search.return_value = ("results", None)
with patch("litellm.callbacks", [logger]):
# Simulate the caller having derived custom_llm_provider from
@ -460,43 +466,6 @@ class TestShortCircuitEntryPoint:
text_block = next(b for b in result["content"] if b["type"] == "text")
assert text_block["text"] == "results"
@pytest.mark.asyncio
@pytest.mark.parametrize(
"raw_prompt,expected_query",
[
(
"Perform a web search for the query: LiteLLM latest version release",
"LiteLLM latest version release",
),
(
'Perform a web search for the query: "who is the maintainer of litellm"',
"who is the maintainer of litellm",
),
(
'"LiteLLM latest version release"',
'"LiteLLM latest version release"',
),
],
)
async def test_short_circuits_strips_claude_code_instructional_prefix(self, raw_prompt: str, expected_query: str):
"""Verify end-to-end that try_short_circuit_search forwards the cleaned query to search."""
logger = WebSearchInterceptionLogger(enabled_providers=["github_copilot"])
with patch.object(logger, "_execute_search", new_callable=AsyncMock) as mock_search:
mock_search.return_value = ("Results", None)
result = await logger.try_short_circuit_search(
model="github_copilot/claude-sonnet-4",
messages=[{"role": "user", "content": raw_prompt}],
tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 8}],
custom_llm_provider="github_copilot",
)
assert result is not None
mock_search.assert_called_once_with(expected_query)
tool_use_block = next(b for b in result["content"] if b["type"] == "server_tool_use")
assert tool_use_block["input"]["query"] == expected_query
class TestShortCircuitSafeguardResults:
"""A short-circuited reply never reaches the backend, so the proxy answers Claude Code's safeguards itself."""