From bdc348f3691f54b22a0d0ee61e2a9a9e57c4b0ec Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Sat, 7 Feb 2026 10:57:57 -0800 Subject: [PATCH] fix: handle empty web_search_options dict edge case Use key presence check instead of truthiness so that kwargs={"web_search_options": {}} produces WebSearchOptions with defaults rather than None. --- .../llm_cost_calc/tool_call_cost_tracking.py | 6 ++---- .../llm_cost_calc/test_tool_call_cost_tracking.py | 8 ++++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/litellm/litellm_core_utils/llm_cost_calc/tool_call_cost_tracking.py b/litellm/litellm_core_utils/llm_cost_calc/tool_call_cost_tracking.py index 99f269758fc..28c14a747e5 100644 --- a/litellm/litellm_core_utils/llm_cost_calc/tool_call_cost_tracking.py +++ b/litellm/litellm_core_utils/llm_cost_calc/tool_call_cost_tracking.py @@ -727,10 +727,8 @@ class StandardBuiltInToolCostTracking: web_search_options: Optional[WebSearchOptions] = None file_search: Optional[FileSearchTool] = None - # Check direct web_search_options first - web_search_options_dict = kwargs.get("web_search_options") - if web_search_options_dict: - web_search_options = WebSearchOptions(**web_search_options_dict) + if "web_search_options" in kwargs: + web_search_options = WebSearchOptions(**kwargs["web_search_options"]) # Get tools once tools = kwargs.get("tools") diff --git a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py index de77cea92a5..54d12dcf506 100644 --- a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py +++ b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_tool_call_cost_tracking.py @@ -376,3 +376,11 @@ class TestGetBuiltInToolsFromKwargs: # Direct web_search_options should take precedence assert web_search is not None assert web_search.get("search_context_size") == "high" + + def test_empty_web_search_options_dict_returns_web_search_options(self): + """An empty web_search_options dict should still produce WebSearchOptions (use defaults).""" + kwargs = {"web_search_options": {}} + web_search, file_search = StandardBuiltInToolCostTracking.get_built_in_tools_from_kwargs(kwargs) + + assert web_search is not None + assert file_search is None