litellm/tests/unit/integrations/websearch_interception
ellisc42 3e9c97e1d2
fix(messages): answer 400 for a non-string user or system text block and keep count_tokens in step with inference (#54165)
* fix(anthropic): answer 400 for non-string user text on /v1/messages and count_tokens unless drop_params

* fix(anthropic): answer 400 when dropping blocks leaves no message

When a /v1/messages or /v1/messages/count_tokens request carried messages but
the empty-block cleanup and the drop_params removal of non-string user text
left none, LiteLLM sent an empty list upstream (Anthropic and OpenAI 400s that
name fields the user never sent), raised a 500 on the chat-completions bridge,
and count_tokens answered 200 with a count. prepare_native_messages, which runs
before the native path and both bridges, now answers 400 invalid_request_error
before any provider call, and the count path answers the same 400. A request
that sent no messages keeps its current behavior

* fix(rust-bridge): keep malformed user text on the Python messages path

With Rust opted in, a null or non-string user text block went to the
Rust handler, which strips it and answers 200. Python answers 400 for
it unless drop_params drops it, so route those requests to Python.

* fix(rust-bridge): apply the malformed user text rule before sync Rust messages calls

Sync anthropic messages calls have no Python path, so routing them off Rust for a
malformed user text block raised "not implemented for sync calls". The sync call now
stays on Rust and drops the block under drop_params or answers the 400 first, while
async calls keep running on the Python path

* fix(rust-bridge): apply the malformed user text rule where the native messages call reads its request

The sync pre-step ran before the native logging setup, so its 400 fired no failure callback, a stray non-object message raised a raw ValidationError under drop_params, and the error carried an empty llm_provider. The Python host now owns the rule at projection time for sync and async native calls, which replaces the async-only routing feature.

* perf(rust-bridge): run the malformed user text rule only when a text block is not a string

The native messages projection called the Python rule on every request, so a
well-formed 501-turn request paid about 2 ms for a scan that found nothing.
Rust now checks the converted messages first, user text and tool_result text
included, and calls the rule only when some text value is not a string.

* fix(rust-bridge): drop non-string user text under drop_params and reject emptied messages natively

The native /v1/messages route now matches Python for a user text block whose text is not a string. Without drop_params Rust sends it as is, so the provider answers its own 400 as on the Python path. With drop_params the block goes, a user turn left empty goes, and a request left with no message answers the same 400 Python raises, naming the model and provider as the caller sent them

Blank text is still stripped in every role and non-string assistant text still goes. A null text no longer parses as an absent one, so a message holding it stays on the wire as sent

The sync native route runs the same cleanup first, so litellm.anthropic.messages.create answers that 400 before its not-implemented error. The count_tokens test merged from main now expects the inference 400 for a body with only empty text, and two branch count_tokens tests pass the response argument main added

* fix(messages): keep empty text blocks in place so the non-string text 400 names the client's own index

A request about to be refused for a non-string user text no longer has its empty text blocks stripped first, on both
the Python and Rust paths and in the count_tokens view. Stripping shifted every later block index by one, so the 400
named the wrong block whenever a blank text came before the bad one. drop_params still strips both

* fix(messages): answer the non-string text 400 locally on every provider so inference and count_tokens agree

Forwarding a null user text as sent only produced a 400 where the provider refuses null. MiniMax and Tencent accept it, so inference answered 200 while count_tokens answered 400, and Anthropic's own 400 came back wrapped on inference and bare on count_tokens

The Rust messages shaping and the Python native entry now answer the same local 400 before any provider call when drop_params is off, naming the field in the client's own body. count_tokens answers it before any count API, tool_result parts included

* fix(messages): keep tool_result text on main's path and answer the local 400 only for top-level user text

The previous commit also answered the local 400 for a malformed text inside a tool_result, which broke main's contract for it. Native Anthropic forwards that text as sent, so a rewriting guardrail can put back each text it left unchanged and a masking guardrail can still scan it, and the OpenAI and chat bridges answer their own 400

The Rust messages shaping, the Python native entry and the count route's early check now cover top-level user text blocks only, and count_tokens keeps main's late tool_result check

* fix(messages): name the resolved provider on the native non-string text 400, as Python does

The Rust route's local 400 named an empty provider for a model given as anthropic/<model>
and logged its failure without a traceback, while Python names the provider and raises it.
Also drops the parity case that pinned main's send-as-is for a numeric user text block,
which this branch answers with the 400 its FALLBACK_CASES already compare with Python

* fix(messages): keep main's cleanup and web search short-circuit beside a non-string tool_result text

A non-string text inside a tool_result skipped the empty-block strip for the whole request on Python-path providers, and turned off the web search short-circuit. Both now run as on main, and only a top-level user text skips the short-circuit, since the route rejects or drops it

* fix(messages): keep main's handling for a blank-only body and forward a non-string system text

* fix(messages): answer 400 for a non-string system text and drop it under drop_params

A system prompt text block whose text is not a string now gets the same local 400 as a user text block, naming system.N.text, on /v1/messages and count_tokens. With drop_params on, the block is dropped, and a system prompt left with no blocks is omitted

* test(messages): pin the user text 400 ahead of a non-string system text

* test(messages): run the user-before-system text order test under rstest on both shaping paths

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-25 00:48:41 +00:00
..
__init__.py test: move tests/test_litellm integrations and secret_managers into tests/unit (#43194) 2026-09-25 12:57:07 -07:00
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: move tests/test_litellm integrations and secret_managers into tests/unit (#43194) 2026-09-25 12:57:07 -07:00
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(messages): answer 400 for a non-string user or system text block and keep count_tokens in step with inference (#54165) 2026-11-25 00:48:41 +00: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