fix(websearch): recognize conventional function tool name

This commit is contained in:
lei_lei 2026-08-30 10:49:44 +08:00 • committed by lei_lei
parent c2c2a623c0
commit f2b399d451
2 changed files with 38 additions and 4 deletions

View file

@ -156,12 +156,12 @@ def is_web_search_tool_chat_completion(tool: dict[str, Any]) -> bool:
"""
Check if a tool is a web search tool for Chat Completions API (strict check).
This is a stricter version that ONLY checks for the exact LiteLLM web search tool name.
This is a stricter version that checks the recognized web search tool names.
Use this for Chat Completions API to avoid false positives with user-defined tools.
Detects ONLY:
- LiteLLM standard: name == "litellm_web_search" (Anthropic format)
- OpenAI format: type == "function" with function.name == "litellm_web_search"
- OpenAI format: type == "function" with function.name == "litellm_web_search" or "web_search"
Args:
tool: Tool dictionary to check
@ -186,7 +186,7 @@ def is_web_search_tool_chat_completion(tool: dict[str, Any]) -> bool:
if tool_type == "function" and "function" in tool:
function_def: Final = tool.get("function", {})
function_name: Final = function_def.get("name", "")
if function_name == LITELLM_WEB_SEARCH_TOOL_NAME:
if function_name in (LITELLM_WEB_SEARCH_TOOL_NAME, "web_search"):
return True
# Check for LiteLLM standard tool (Anthropic format)
@ -270,7 +270,7 @@ def is_web_search_tool(tool: dict[str, Any]) -> bool:
if tool_type == "function" and "function" in tool:
function_def: Final = tool.get("function", {})
function_name: Final = function_def.get("name", "")
if function_name == LITELLM_WEB_SEARCH_TOOL_NAME:
if function_name in (LITELLM_WEB_SEARCH_TOOL_NAME, "web_search"):
return True
# Check for LiteLLM standard tool (Anthropic format)

View file

@ -0,0 +1,34 @@
"""Unit tests for web search tool shape detection."""
import pytest
from litellm.integrations.websearch_interception.tools import (
is_web_search_tool,
is_web_search_tool_chat_completion,
)
@pytest.mark.parametrize(
"tool",
[
{"type": "function", "function": {"name": "litellm_web_search"}},
{"type": "function", "function": {"name": "web_search"}},
],
)
def test_openai_function_web_search_shapes_are_detected(tool):
"""Recognize both LiteLLM and conventional web_search function names."""
assert is_web_search_tool(tool) is True
assert is_web_search_tool_chat_completion(tool) is True
@pytest.mark.parametrize(
"tool",
[
{"type": "function", "function": {"name": "web_search_helper"}},
{"type": "function", "function": {"name": "search"}},
],
)
def test_unrelated_openai_function_tools_are_not_detected(tool):
"""Do not classify similarly named user tools as web search."""
assert is_web_search_tool(tool) is False
assert is_web_search_tool_chat_completion(tool) is False