litellm/tests/unit/proxy/utils
srik-straiker f92a40faa6
feat(guardrails): hold streamed tool calls for the verdict and log prompts blocked with a reply (#51872)
* feat(guardrails): opt-in hold of streamed tool calls until the end-of-stream verdict

A guardrail that checks a live stream once, at its end (streaming_end_of_stream_only
without streaming_buffer_until_moderated), releases every chunk as it arrives and
holds back only the stream's final frames. A tool call in that stream reaches the
client before the verdict exists, so a client that runs tools has already run it
when the guardrail blocks.

streaming_hold_tool_calls is a new streaming flag, off by default and read the same
way as the other streaming flags. With it on, the shared streaming loop also holds
everything from the first item that starts a tool call the client would run. Text
before the tool call still streams live. The held items are released in order when
the check passes and dropped when it blocks, through the handling the final frames
already had.

The hold applies in every mode that releases a stream before its end-of-stream
verdict: scanned once at its end, scanned every few chunks in the foreground or in
the background, and the incremental text transform, which keeps back its raw
tool-call chunks until its end-of-stream block inspection. A stream buffered until
moderated already waits for a scan and is unchanged.

A held tool call is released only on a verdict that covered it. A stream that stops
without its terminal marker (an upstream error, a cut connection) is scanned closed
the way its endpoint ends a stream, as it already is after a client disconnect.

Each endpoint translation answers starts_client_tool_call for one stream item, so
nothing re-walks the stream: chat completions, messages, responses (every output
item the client runs) and the Bedrock pass-through frames, Converse and invoke.
The base answer is False: a guardrail that does not set the flag, and an endpoint
with no answer of its own, behave as before.

With the flag on, the loop reads the stream in whole frames. Native /v1/messages
relays raw transport reads, so an SSE event can arrive split across two of them,
which hides the start of a tool call, and the end of the stream, from a check that
reads one item at a time. split_whole_stream_frames cuts each read at the last
frame boundary and carries the rest into the next read. The client receives the
same bytes.

The Straiker guardrail sets the flag when stream_mode is "streaming". "buffered"
already holds the whole response and is unchanged.

* feat(guardrails): opt-in failure logging for a prompt blocked with a reply

A guardrail that blocks the prompt by raising ModifyResponseException is answered
with an HTTP 200 reply. The LLM is never called and the reply runs no success
logging, so the block reaches no logging callback on non-streamed chat completions,
messages, responses or text completions. Only a streamed chat completion logs it,
as a success event from the stream wrapper.

ModifyResponseException takes a new log_as_failure argument, off by default. When
a block sets it, post_call_failure_hook treats it like the other proxy-only errors
and sends one failure event to the logging callbacks.
The streamed chat completion path tells the hook its reply already logs a success
event, so that route still logs exactly one event.

A guardrail that does not set it behaves as before. Blocked answers and the spend
logs are unchanged.

The Straiker guardrail sets it on the reply it raises for a blocked prompt.

* fix(guardrails): always run the end-of-stream round while a tool call is held

The end-of-stream round was skipped as redundant when its scan key equalled the
key of the last sampled round. A scan key carries only what a translation
fingerprints, so with a held item it does not fingerprint (a Responses
shell_call, apply_patch_call or computer_call), the keys matched and the item was
released with no round after it arrived. The round now always runs while a tool
call is held, so a guardrail that reads the whole response judges the item.

The whole-frame split goes through a typed translation variable. The untyped
lookup it used left three later call sites partially unknown to the type checker.

Tests: the blocked-prompt logging test waits for the callback instead of a fixed
delay, and the new recording callback and its fixtures are typed.

* fix(guardrails): hold the whole stream on a route that cannot tell a tool call apart

With streaming_hold_tool_calls on, a translation with no tool-call detection, the
native Gemini generateContent one for example, answered that no stream item starts
a tool call, so a functionCall part reached the client before the end-of-stream
verdict.

The base answer of starts_client_tool_call is now True: a translation that cannot
tell a tool call apart holds its whole stream for the verdict. Text completions
answer False, since they carry no tool calls. A pass-through stream no translation
reads is not scanned at all, so it is still not held.

A guardrail that does not set the flag is unaffected.

* feat(straiker): hold_tool_calls option to turn the streamed tool-call hold off

In stream_mode `streaming` the guardrail holds a tool call the client would
run until the end-of-stream verdict. That was fixed on, with no way out for a
deployment that prefers tool calls to stream as they arrive.

`hold_tool_calls` (default true) is read only in `streaming` mode. False
releases tool calls as they arrive, as before the hold existed. A value the
initializer cannot read keeps the hold and logs a warning: raising there
would start the proxy without the guardrail.

* feat(guardrails): tell a function call apart on Gemini streams, so only it is held

With the tool-call hold on, the native Gemini generateContent route held its
whole stream for the verdict, because its translation could not tell a tool
call apart. Text that streamed live before arrived in one burst at the end.

The translation now recognises a functionCall part. Text streams live and
everything from the first function call waits for the verdict. An item it
cannot read for a function call (a cut event, a payload that is not a JSON
object) still waits.

A translation that never scans a stream gives no verdict to hold for, so the
base answer now holds a whole stream only for a translation that scans it.

* test(guardrails): type the new test helpers and drop a banned suppression

The hold_tool_calls tests in test_straiker.py left parameters and return
types unannotated, and the Gemini detection table used `# type: ignore`,
which the repository bans. The fixture now builds the second candidate with
a typed helper instead of indexing into a dict.
2026-11-20 16:58:15 -08:00
..
helpers fix(responses): run the guardrails a Responses WebSocket frame selects (#48725) 2026-10-30 14:12:51 -07:00
prisma_and_spend perf(proxy): coalesce empty auto-router baseline recovery polls (#52523) 2026-11-19 00:20:59 +00:00
proxy_logging feat(guardrails): hold streamed tool calls for the verdict and log prompts blocked with a reply (#51872) 2026-11-20 16:58:15 -08:00
__init__.py