litellm/tests/unit/proxy/policy_engine
quietjules 9eb867b5aa
fix(guardrails): mask reasoning text on /v1/responses output (#52504)
* fix(guardrails): mask reasoning text on /v1/responses output

Post-call masking guardrails only saw the answer on /v1/responses, so a
reasoning summary, the Claude bridge's reasoning content, and the thinking
text inside its encrypted_content and provider_specific_fields reached the
client raw. Scan those texts with the answer, answers first, and write the
mask back to the response and every stream event. Thinking signatures stay
untouched so the blob still replays.

* fix(guardrails): scan reasoning text apart from the answer

A guardrail that reads one end of its text list overwrote the answer with masked reasoning when both shared one call. Each distinct reasoning text now gets its own apply_guardrail call after the answer rewrite, and whole-response scanners keep one call.

* test(guardrails): pin reasoning call inputs and the stream fallback

* fix(guardrails): scan streamed reasoning a cut-short envelope lacks and send reasoning in one call

A stream cut short by max_output_tokens can end with a terminal envelope that
lacks reasoning it already streamed, so that reasoning skipped BLOCK and MASK.
It now gets its own guardrail call and the mask reaches its stream events.

Every distinct reasoning text now goes to the guardrail in one call instead of
one call per text. A guardrail that returns a different number of texts falls
back to one call per text so each text keeps its own rewrite.

* fix(guardrails): scan each reasoning text on its own unless the guardrail batches, keep the judge on the answer

A guardrail that reads one end of its text list now checks every reasoning text, the content filter keeps one call for all of them, llm_as_a_judge judges the answer alone, and a legacy hook keeps its answer mask when reasoning streams past a cut-short final event

* fix(guardrails): mask reasoning text a content_part.added event opens with

* fix(guardrails): send all reasoning to a guardrail in one call and let the judge read reasoning

Reasoning texts go to a guardrail in one call after the answer by default. Guardrails that read one end of texts (Qualifire, HiddenLayer, Gray Swan, Ovalix, custom code, CollieAI, Bedrock latest-message-only) opt out through scans_each_text_separately and get one call per text. A batch answered with a different number of texts is re-sent one text per call.

llm_as_a_judge takes the answer and the reasoning in one call through scans_all_response_texts_in_one_call, and scans_reasoning is removed

* fix(guardrails): mask reasoning parts only a finished item holds and provider thinking in an empty output

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-19 01:25:06 -08:00
..
__init__.py
test_attachment_registry.py test(proxy): make the stagger-offset and linear-dedup guards independent of runner identity and load (#45031) 2026-10-07 04:07:36 -07:00
test_condition_evaluator.py
test_init_policies.py feat(proxy)!: add suppress_startup_output to turn off startup prints (#47708) 2026-10-29 03:47:04 -07:00
test_pipeline_executor.py fix(guardrails): mask reasoning text on /v1/responses output (#52504) 2026-11-19 01:25:06 -08:00
test_policy_engine_endpoints.py
test_policy_matcher.py
test_policy_registry.py chore(typing): clear 670 basedpyright Any errors by validating provider payloads at the boundary (#45388) 2026-10-13 15:35:23 -07:00
test_policy_resolver.py
test_policy_validator.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_policy_versioning.py
test_policy_versioning_e2e.py
test_response_retrieval.py refactor(router): move router callers onto the entry points and invert the proxy imports (#48847) 2026-10-29 17:26:33 +00:00