The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix: use original_stream for short-circuit, propagate derived provider
Jonathan Barazany3b129260f557
diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py b/litellm/llms/anthropic/experimental_pass_through/messages/handler.pyindex 52373a9c327..dcd9214cf8c 100644--- a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py+++ b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py@@ -186,6 +186,12 @@ async def anthropic_messages("""Async: Make llm api request in Anthropic /messages API spec"""+ # Save original stream flag before pre-request hooks can convert it.+ # The websearch interception hook converts stream=True → stream=False+ # for the agentic loop, but the short-circuit path needs to know+ # whether the caller originally requested streaming.+ original_stream = stream+# Execute pre-request hooks to allow CustomLoggers to modify requestrequest_kwargs = await _execute_pre_request_hooks(model=model,@@ -199,6 +205,11 @@ async def anthropic_messages(# Extract modified parameterstools = request_kwargs.pop("tools", tools)stream = request_kwargs.pop("stream", stream)+ # Propagate the provider derived inside pre-request hooks, if not already set+ if not custom_llm_provider:+ custom_llm_provider = request_kwargs.get("litellm_params", {}).get(+ "custom_llm_provider"+ )# Remove litellm_params from kwargs (only needed for hooks)request_kwargs.pop("litellm_params", None)# Merge back any other modifications@@ -207,12 +218,14 @@ async def anthropic_messages(# Short-circuit web-search-only requests: detect the pattern, execute# search directly via Tavily/Perplexity, and return a synthetic response# without ever touching the backend LLM or the adapter path.+ # Use original_stream (not the hook-converted stream) so streaming+ # callers get SSE events instead of a plain dict.short_circuit_response = await _try_websearch_short_circuit(model=model,messages=messages,tools=tools,custom_llm_provider=custom_llm_provider,- stream=stream,+ stream=original_stream,)if short_circuit_response is not None:return short_circuit_responsediff --git a/tests/test_litellm/integrations/websearch_interception/test_websearch_short_circuit.py b/tests/test_litellm/integrations/websearch_interception/test_websearch_short_circuit.pyindex 68d2276385d..3e2c85074b6 100644--- a/tests/test_litellm/integrations/websearch_interception/test_websearch_short_circuit.py+++ b/tests/test_litellm/integrations/websearch_interception/test_websearch_short_circuit.py@@ -342,3 +342,67 @@ class TestShortCircuitEntryPoint:stream=False,)assert result is None++ @pytest.mark.asyncio+ async def test_uses_original_stream_not_hook_converted(self):+ """Verify that the entry point passes original_stream to the short-circuit.++ The pre-request hook converts stream=True → stream=False for the agentic+ loop. The short-circuit must use the ORIGINAL stream value so streaming+ callers get SSE events instead of a plain dict.+ """+ from litellm.llms.anthropic.experimental_pass_through.messages.fake_stream_iterator import (+ FakeAnthropicMessagesStreamIterator,+ )+ from litellm.llms.anthropic.experimental_pass_through.messages.handler import (+ _try_websearch_short_circuit,+ )++ logger = WebSearchInterceptionLogger(enabled_providers=["github_copilot"])+ with patch.object(+ logger, "_execute_search", new_callable=AsyncMock+ ) as mock_search:+ mock_search.return_value = "streaming results"+ with patch("litellm.callbacks", [logger]):+ # Simulate what anthropic_messages() does: original_stream=True+ # is passed to the short-circuit, even though the hook would have+ # already converted stream to False in request_kwargs.+ result = await _try_websearch_short_circuit(+ model="github_copilot/claude-sonnet-4",+ messages=[{"role": "user", "content": "search query"}],+ tools=[{"type": "web_search_20250305", "name": "web_search"}],+ custom_llm_provider="github_copilot",+ stream=True, # original_stream, NOT the hook-converted value+ )++ # Must return a stream iterator, not a plain dict+ assert isinstance(result, FakeAnthropicMessagesStreamIterator)++ @pytest.mark.asyncio+ async def test_short_circuits_with_provider_from_model_string(self):+ """Provider embedded in model string (custom_llm_provider=None) should+ still fire the short-circuit when the caller propagates the derived+ provider.+ """+ from litellm.llms.anthropic.experimental_pass_through.messages.handler import (+ _try_websearch_short_circuit,+ )++ logger = WebSearchInterceptionLogger(enabled_providers=["github_copilot"])+ with patch.object(+ logger, "_execute_search", new_callable=AsyncMock+ ) as mock_search:+ mock_search.return_value = "results"+ with patch("litellm.callbacks", [logger]):+ # Simulate the caller having derived custom_llm_provider from+ # the model string before calling _try_websearch_short_circuit+ result = await _try_websearch_short_circuit(+ model="github_copilot/claude-sonnet-4",+ messages=[{"role": "user", "content": "search query"}],+ tools=[{"type": "web_search_20250305", "name": "web_search"}],+ custom_llm_provider="github_copilot",+ stream=False,+ )++ assert result is not None+ assert result["content"][0]["text"] == "results"