litellm/tests/unit/responses
quietjules 25b010e8e1
fix(guardrails): scan refusal text in output guardrails on chat and responses (#54039)
* fix(guardrails): scan refusal text on chat and Responses output

Output guardrails only read message content and output_text parts, so a model
refusal reached the client unmasked and a blocking guardrail let it through.
Presidio, Bedrock Guardrails, Model Armor and the unified handlers now read and
rewrite chat message.refusal, streamed delta.refusal, Responses refusal parts
and refusal stream events, on HTTP and on WebSocket Responses.

* fix(guardrails): write Model Armor redactions into a single-part Responses body

Model Armor returns one sanitized string for everything it scanned, so a Responses
body answered 400 and a stream was blocked whenever masking had to rewrite it. When
the scan came from one content part, write the string into that part and sync the
buffered stream events. A body that joined several parts or tool-call arguments has
no single place for the string and stays blocked.

* fix(guardrails): keep Model Armor's non-streamed Responses scan on message text

A masking Model Armor answered 400 on a Responses body with an email in forced tool call arguments,
where main answers 200. Scan only message text on that path, since one sanitized string cannot be
written back into tool call arguments, and spread that string over every part so a body with several
parts is masked instead of blocked

* fix(logging): redact chat refusal text when message logging is off

* fix(guardrails): keep Model Armor masks on answer parts and write back emptied refusals

A refusal a guardrail empties on an ended chat stream is now written back as an empty string instead of escaping raw

Model Armor on non-streamed Responses scans tool call arguments when it blocks, masks only answer message parts so a bridged reasoning item keeps its own text, and blocks a stream whose text events fall outside message parts. A chat choice with both content and a refusal gets the masked text once, in content

* fix(guardrails): check Armor tool args in mask mode and content filter streamed refusals

Model Armor masking on non-streamed Responses now scans tool call arguments and reasoning too, so its
URI, jailbreak and RAI filters block a flagged argument. It scans the message text again alone when the
two differ, so the mask lands only on the answer

The content filter's chat stream hook keeps one holdback per choice and field, so a streamed refusal is
masked or blocked like content

The shared chat stream scan skips relayed error strings instead of crashing on them, and the Rust spend
log redaction covers the refusal field like the Python one

* fix(guardrails): block a Responses answer that Armor's message-only rescan flags

When Model Armor masks a non-streamed Responses body that also carries tool calls or reasoning, it scans the
message text again alone to place its mask. That second scan now blocks the response when it flags the text,
instead of only reading its masked copy

Adds a regression test that a Presidio Responses stream cut short in the middle of a tool call ends with an
error before any event goes out, so its raw arguments never reach the client

* fix(guardrails): block Model Armor's message-only rescan when it matches without a rewrite

Model Armor's second scan of non-streamed Responses message text now answers 400 when it
reports a de-identify match but returns no rewrite, as the shared guardrail path already
does, instead of sending the matched text out unchanged. The rescan also writes its own
guardrail log entry, so a block from it no longer reads as success

Presidio already ends a Responses stream when the analyzer cannot be reached, and a
regression test now pins that. A streamed chunk builder test that passed at the
merge base is removed, since it caught nothing

* fix(guardrails): keep response text out of the shared Responses output debug log

The shared Responses output extractor logged each whole output item at debug level, so
with debug logging on, the answer text that Model Armor now scans on non-streamed
Responses reached the logs even with sanitize_error_detail on. The line now logs only
the item index and its part count

* fix(guardrails): block a non-streamed Responses Armor match that comes back without a rewrite

Under a masking Model Armor guardrail, a non-streamed Responses scan that reports a
de-identify match but returns no rewrite now answers 400, as the message-only rescan and
the stream path already do, instead of sending the matched text out unchanged. Chat keeps
main's behavior there, since failing it would turn requests main answers with 200 into
errors

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-24 05:27:45 -08:00
..
litellm_completion_transformation fix(openai): relay provider error codes, types, and Responses params from OpenAI-compatible backends (#51809) 2026-11-24 02:23:00 +00:00
mcp feat(responses): store each bridged turn's path to the root so a fork replays only its ancestors (#49519) 2026-11-23 12:43:11 -08:00
__init__.py
test_additional_tools.py feat(proxy): mixed Codex model picker and LiteLLM routing on native codex routes (#49408) 2026-11-06 11:14:47 -08:00
test_custom_tool_call.py
test_input_items_query.py fix(proxy): forward limit, order, after, before and include on GET /v1/responses/{id}/input_items (#51259) 2026-11-20 07:54:26 -08:00
test_instructions_length.py fix(responses): send bridged system prompts too long for instructions as input (#52964) 2026-11-20 11:56:47 -08:00
test_metadata_codex_callback.py
test_no_duplicate_spend_logs.py
test_null_test_fix.py
test_responses_api_bridge_flag.py fix(llms): treat chat-latest and gpt-chat-latest as gpt-5 family models (#51151) 2026-11-24 04:02:51 -08:00
test_responses_api_lifecycle.py fix(responses): log the resolved provider on Responses API calls (#52622) 2026-11-18 22:34:16 +00:00
test_responses_api_request_body.py fix(router): count a Responses WebSocket turn's input against deployment ITPM (#53594) 2026-11-24 04:26:36 -08:00
test_responses_prompt_management.py fix(sdk): decide MCP gateway routing without importing the proxy handler (#45961) 2026-10-20 01:44:54 +00:00
test_responses_router_cooldown.py fix(openai): relay provider error codes, types, and Responses params from OpenAI-compatible backends (#51809) 2026-11-24 02:23:00 +00:00
test_responses_streaming_iterator.py fix(responses): resume background response streams with stream and starting_after (#48819) 2026-10-29 02:31:22 -07:00
test_responses_supported_endpoints_passthrough.py
test_responses_utils.py fix(openai): forward Responses background_options (#53991) 2026-11-23 17:56:11 -08:00
test_responses_websocket_all_providers.py fix(guardrails): scan refusal text in output guardrails on chat and responses (#54039) 2026-11-24 05:27:45 -08:00
test_rust_bridge_websocket.py refactor(rust-bridge): require the native extension (#48852) 2026-10-28 13:16:56 -07:00
test_sse_output_recovery.py
test_streaming_iterator.py fix(responses): always stream tool argument fields as strings for every Responses provider (#53953) 2026-11-24 03:45:17 +00:00
test_streaming_iterator_error_events.py fix(openai): relay provider error codes, types, and Responses params from OpenAI-compatible backends (#51809) 2026-11-24 02:23:00 +00:00
test_text_format_conversion.py refactor: expose core private helpers under public names (#44871) 2026-10-07 00:05:27 +00:00