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
refactor: reuse get_last_user_message, fix UUID convention, move import
Jonathan Barazany141ad0495520
diff --git a/litellm/integrations/websearch_interception/handler.py b/litellm/integrations/websearch_interception/handler.pyindex 59510dd5d08..b557d8c0e77 100644--- a/litellm/integrations/websearch_interception/handler.py+++ b/litellm/integrations/websearch_interception/handler.py@@ -8,6 +8,7 @@ server-side using litellm router's search tools.import asyncioimport math+import uuidfrom typing import Any, Dict, List, Optional, Tuple, Union, castimport litellm@@ -110,7 +111,11 @@ class WebSearchInterceptionLogger(CustomLogger):return None# Extract search query from the last user message- query = self._extract_search_query(messages)+ from litellm.litellm_core_utils.prompt_templates.common_utils import (+ get_last_user_message,+ )++ query = get_last_user_message(messages)if not query:return None@@ -129,10 +134,8 @@ class WebSearchInterceptionLogger(CustomLogger):search_result_text = f"Search failed: {e}"# Build synthetic Anthropic response- from uuid import uuid4-response: Dict[str, Any] = {- "id": f"msg_{uuid4().hex[:24]}",+ "id": f"msg_{str(uuid.uuid4())}","type": "message","role": "assistant","model": model,@@ -148,39 +151,6 @@ class WebSearchInterceptionLogger(CustomLogger):)return response- @staticmethod- def _extract_search_query(messages: List[Dict]) -> Optional[str]:- """- Extract the search query from messages.-- Looks at the last user message content for the search query text.- """- if not messages:- return None-- # Find the last user message- for msg in reversed(messages):- if msg.get("role") != "user":- continue-- content = msg.get("content")- if isinstance(content, str):- return content.strip() or None-- # Handle list-of-blocks content- if isinstance(content, list):- for block in content:- if isinstance(block, dict) and block.get("type") == "text":- text = block.get("text", "").strip()- if text:- return text- elif isinstance(block, str):- text = block.strip()- if text:- return text-- return None-async def async_pre_call_deployment_hook(self, kwargs: Dict[str, Any], call_type: Optional[Any]) -> Optional[dict]:diff --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 3e2c85074b6..cb90b254e40 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@@ -187,53 +187,6 @@ class TestTryShortCircuitSearch:# ----------------------------------------------------------------------------class TestExtractSearchQuery:- """Tests for WebSearchInterceptionLogger._extract_search_query"""-- def test_string_content(self):- messages = [{"role": "user", "content": "Search for Python 3.14 features"}]- assert (- WebSearchInterceptionLogger._extract_search_query(messages)- == "Search for Python 3.14 features"- )-- def test_list_content_with_text_block(self):- messages = [- {- "role": "user",- "content": [- {"type": "text", "text": "Perform a web search for latest news"},- ],- }- ]- assert (- WebSearchInterceptionLogger._extract_search_query(messages)- == "Perform a web search for latest news"- )-- def test_takes_last_user_message(self):- messages = [- {"role": "user", "content": "First message"},- {"role": "assistant", "content": "Response"},- {"role": "user", "content": "Second message"},- ]- assert (- WebSearchInterceptionLogger._extract_search_query(messages)- == "Second message"- )-- def test_empty_messages(self):- assert WebSearchInterceptionLogger._extract_search_query([]) is None-- def test_no_user_messages(self):- messages = [{"role": "assistant", "content": "Hello"}]- assert WebSearchInterceptionLogger._extract_search_query(messages) is None-- def test_empty_content(self):- messages = [{"role": "user", "content": ""}]- assert WebSearchInterceptionLogger._extract_search_query(messages) is None--# ---------------------------------------------------------------------------# Integration with entry point# ---------------------------------------------------------------------------