litellm/tests/unit/integrations/websearch_interception
martenj4 8ef5cf1f25
fix(websearch_interception): answer a web search turn that reaches max_agentic_loops (#54053)
* fix(websearch_interception): answer from gathered results on the last follow-up the loop cap allows

When a web search turn reached max_agentic_loops, the last follow-up still
offered the search tool, so the model asked for one more search, the loop
refused it, and the user got no answer (an empty end_turn on /v1/messages and
a dangling litellm_web_search call on chat and responses).

The follow-up the cap allows last now drops the intercepted search tool and
keeps client tools. When no tools remain it sends tool_choice none, because
Claude otherwise calls the search tool it saw in the history. This applies to
anthropic, azure_ai, vertex_ai, openai and gemini, the providers probed live.
Bedrock and other providers keep today's behavior.

The deployment hook now passes the callback's max_agentic_loops into the call,
so the dispatchers and the builder agree on the ceiling. Chat follow-ups carry
the loop depth in litellm_metadata, so the openai and http chat paths can see
it.

* fix(websearch_interception): answer a capped turn with a max_uses_exceeded result and one last call without search

Claude ended a capped turn with no text when the last allowed follow-up simply
dropped the search tool, since it still wanted to search. The dispatchers now
let a callback that opts in through answers_tool_calls_at_agentic_loop_cap run
once more at the cap. The websearch hook answers the refused search calls with
a max_uses_exceeded search result instead of searching, and sends one last call
without the search tool (client tools kept, tool_choice none when nothing is
left). Bedrock is not opted in and keeps the old refusal.

The three per-surface search loops are now one helper.

* fix(anthropic messages): keep the client's max_tokens on every agentic follow-up

The request transform popped max_tokens off the params dict the agentic hooks
read afterwards, so every /v1/messages follow-up fell back to 1024 and a capped
turn could stop at max_tokens before answering. The handler now gives the
transform its own copy.

* fix(websearch_interception): keep the allowed_tools limit on the capped call and strip loop controls from client metadata

* fix(websearch): keep the capped last call from searching again or calling a forced client tool

The refused search now tells the model to answer from the results it has. If the
last call at the cap still asks for a search, the chat and Responses gates claim it
so the loop ends the turn with that call removed instead of handing the client a
tool it never declared. A tool_choice that forces some tool is relaxed to auto on
that call, while a choice naming one client tool stays. ARCHITECTURE.md now says the
feature-level cap covers all three routes and which providers get the last call

* docs(websearch_interception): say the loop cap counts rounds of searching, not single searches

* fix(websearch_interception): keep the one-tool-call limit and drop parallel_tool_calls on the last call at the cap

The tool-free last chat call no longer sends parallel_tool_calls, which OpenAI rejects without tools

A forced choice relaxed to auto keeps disable_parallel_tool_use on /v1/messages, and a choice forcing the search tool becomes auto instead of being dropped

/v1/messages drops a search call the capped call still made, like chat and Responses already do

The chat capped call reads the request's tool_choice the same way the other routes do

* fix(azure_ai): drop tool_choice when a chat request has no tools, as the OpenAI config does

* fix(websearch_interception): keep the feature-level max_agentic_loops on /v1/messages only, as on main

Chat and Responses read the deployment's own max_agentic_loops or the default of 3. Applying the feature-level value there ended turns empty on Bedrock and other providers outside the answer-at-cap list once it was below 3

* test(websearch_interception): type the cap-source parameters of the chat and Responses cap tests

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-24 01:55:38 -08:00
..
__init__.py
conftest.py fix(websearch_interception): answer a web search turn that reaches max_agentic_loops (#54053) 2026-11-24 01:55:38 -08:00
test_websearch_agentic_loop_cap.py fix(websearch_interception): answer a web search turn that reaches max_agentic_loops (#54053) 2026-11-24 01:55:38 -08:00
test_websearch_chat_completion.py fix(websearch_interception): answer a web search turn that reaches max_agentic_loops (#54053) 2026-11-24 01:55:38 -08:00
test_websearch_interception_handler.py refactor(router): move router callers onto the entry points and invert the proxy imports (#48847) 2026-10-29 17:26:33 +00:00
test_websearch_interception_thinking.py
test_websearch_native_blocks.py fix(exa): return highlights, highlight scores, score and the response envelope from Exa search (#47423) 2026-10-19 15:45:56 -07:00
test_websearch_responses.py fix(websearch_interception): answer a web search turn that reaches max_agentic_loops (#54053) 2026-11-24 01:55:38 -08:00
test_websearch_rich_query_shape.py refactor(router): move router callers onto the entry points and invert the proxy imports (#48847) 2026-10-29 17:26:33 +00:00
test_websearch_short_circuit.py fix(websearch-interception)!: stream native server-tool blocks to clients (#40967) 2026-10-30 00:06:57 -07:00
test_websearch_streaming_wrap.py refactor(anthropic): rename llms/anthropic/pass_through to llms/anthropic/messages (#48510) 2026-10-27 10:08:25 -07:00
test_websearch_thinking_constraint.py fix(agentic)!: surface a failed agentic follow-up call instead of the pre-loop tool call (#49227) 2026-11-04 02:00:33 +00:00