diff --git a/litellm/integrations/websearch_interception/handler.py b/litellm/integrations/websearch_interception/handler.py index 0a4759d403c..ca65f114c24 100644 --- a/litellm/integrations/websearch_interception/handler.py +++ b/litellm/integrations/websearch_interception/handler.py @@ -1291,9 +1291,13 @@ class WebSearchInterceptionLogger(CustomLogger): tools_param: Final = optional_params.get("tools") optional_params_clean: Final = { - k: v - for k, v in optional_params.items() - if k not in {"tools", "tool_choice", "stream", "model_alias_map", "stream_response", "custom_prompt_dict"} + **{ + k: v + for k, v in optional_params.items() + if k + not in {"tools", "tool_choice", "stream", "model_alias_map", "stream_response", "custom_prompt_dict"} + }, + **_kept_allowed_tools_tool_choice(_OPTIONAL_PARAMS.validate_python(optional_params)), } kwargs_for_followup: Final = { diff --git a/tests/unit/integrations/websearch_interception/test_websearch_responses.py b/tests/unit/integrations/websearch_interception/test_websearch_responses.py index 0ad758cf53f..01267496668 100644 --- a/tests/unit/integrations/websearch_interception/test_websearch_responses.py +++ b/tests/unit/integrations/websearch_interception/test_websearch_responses.py @@ -364,6 +364,37 @@ async def test_responses_followup_keeps_the_caller_model_across_the_chat_complet assert followup_model == requested_model +@pytest.mark.asyncio +@pytest.mark.usefixtures("perplexity_search") +@pytest.mark.parametrize( + ("tool_choice", "followup_tool_choice"), + [ + ( + {"type": "allowed_tools", "mode": "required", "tools": [{"type": "function", "name": "litellm_web_search"}]}, + {"type": "allowed_tools", "mode": "auto", "tools": [{"type": "function", "name": "litellm_web_search"}]}, + ), + ({"type": "function", "name": "litellm_web_search"}, None), + ("required", None), + ], +) +async def test_responses_followup_keeps_only_an_allowed_tools_choice_without_forcing_another_call( + tool_choice: object, followup_tool_choice: object +) -> None: + plan: Final = await WebSearchInterceptionLogger().async_build_responses_agentic_loop_plan( + tools={**_WEB_SEARCH_FUNCTION_CALL, "provider": "azure"}, + model="gpt-5.4-mini", + messages=[{"role": "user", "content": "What's the latest AI news?"}], + response=_responses_output_with_web_search(), + optional_params={"tools": [{"type": "function", "name": "litellm_web_search"}], "tool_choice": tool_choice}, + logging_obj=None, + stream=False, + kwargs={"custom_llm_provider": "azure", "_agentic_loop_api_surface": RESPONSES_AGENTIC_SURFACE}, + ) + + assert plan.request_patch is not None + assert plan.request_patch.optional_params.get("tool_choice") == followup_tool_choice + + @pytest.mark.asyncio async def test_deployment_hook_converts_native_responses_web_search_tool(): """async_pre_call_deployment_hook converts a native Responses web_search tool