litellm/tests/unit/proxy/guardrails/guardrail_hooks
Agung Besti c482013a8a
fix(guardrails): log requests that a guardrail blocks on /apply_guardrail (#51014)
* fix(guardrails): record the guardrail verdict on a request dict that starts empty

The Bedrock guardrail and the shared `log_guardrail_information` wrapper both picked the request dict with `or {}`. An empty dict is falsy, so a caller that passed one had the verdict written to a throwaway dict. /apply_guardrail passes an empty dict for a request that carries no messages and no metadata, so nothing downstream could see what the guardrail decided. The verdict now goes to the dict the caller passed, and only a missing dict falls back to a new one.

* fix(guardrails): log requests that a guardrail blocks on /apply_guardrail

A request a guardrail blocked on /apply_guardrail was missing from the logs. The endpoint skipped the failure handlers for an HTTPException, the spend log failure hook wrote no row because the route is not an LLM API route, and the guardrail's verdict stayed on a request dict no logger reads.

The endpoint now runs the failure handlers for every exception and copies the verdict onto the logged request, so allowed and blocked requests both carry it along with the guardrail's cost. It marks the failure of a request it had set up with a sentinel, and the spend log failure hook writes its row for that mark. A request rejected before setup, such as one with a bad key or an unknown guardrail, is still not logged. Guardrail records a caller sends in metadata are dropped for both response formats so a verdict cannot be forged in the logs.

* fix(guardrails): keep Presidio and Gray Swan verdicts on empty request data

Presidio's text-only path and Gray Swan's API-failure path replaced an empty request dict with a throwaway one, so the verdict never reached the logged request. The apply_guardrail logging tests now run the real request setup and read what a logging callback receives.

* test(guardrails): cover the endpoint failure row, the sync decorator, and an unreachable Gray Swan without real sockets

* fix(logging): redact prompt text in guardrail records when message logging is off

* fix(logging): redact the failure payload when message logging is off

* fix(logging): keep guardrail details for other callbacks when one opts out of message logging

* fix(logging): redact failure events per callback and keep guardrail compression stats

A callback that turns off message logging now gets the same redacted copy on failure events as on success events, so a blocked /apply_guardrail call no longer hands it the raw prompt and verdict

Guardrail redaction keeps the numeric compression stats (tokens saved, compression ratio) that spend tracking reads, through one helper shared with the spend-log sanitizer

* fix(policy): test each batch input on its own copy of the request data

The guardrail logging decorator now records a verdict on an empty request dict instead of dropping it. The policy test endpoint shared one request dict across every input and merged it into the agent call, so each later input and agent request carried the earlier inputs' verdicts. Each input now gets its own copy, which keeps the caller's request data unchanged

* test(policy): look up the counting guardrail through the real registry

The batch isolation test now registers its guardrail as a litellm callback, so the endpoint finds it through the real guardrail registry instead of a mocked one, and its apply_guardrail override carries the same concrete types as the base class

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-16 15:18:08 -08:00
..
akto refactor(mcp): drop the experimental label from the MCP server and client paths (#48513) 2026-10-27 17:47:50 +00:00
aporia_ai refactor(types): replace Any with proven types in 157 files (#44798) 2026-10-06 15:31:54 +00:00
azure fix(guardrails): restore Azure guardrail get_user_prompt dispatch and allow logging (#44067) 2026-10-01 23:30:32 -07:00
content_filter chore: move ui and vscode-extension under litellm and delete docs (#50992) 2026-11-12 04:19:57 +00:00
dynamoai chore(typing): clear 670 basedpyright Any errors by validating provider payloads at the boundary (#45388) 2026-10-13 15:35:23 -07:00
guardrails_ai refactor(typing): clear about 1,100 basedpyright Any errors across 303 files (#49986) 2026-11-06 13:31:15 -08:00
ibm_guardrails chore(typing): clear 670 basedpyright Any errors by validating provider payloads at the boundary (#45388) 2026-10-13 15:35:23 -07:00
javelin test: move whole-unit legacy test files into tests/unit and delete dead skips (#44807) 2026-10-07 05:02:28 +00:00
litellm_content_filter fix(guardrails): record every content filter match in logging_only and offer the mode in the UI (#46673) 2026-10-16 00:37:03 -07:00
noma fix(guardrails): honor streaming settings on the Noma v2 guardrail (#46606) 2026-10-15 18:29:05 -07:00
openai feat(openai)!: honor per-deployment workload identity on non-chat routes (#39966) 2026-10-16 22:25:15 +00:00
semantic_guard refactor(typing): clear 1181 basedpyright Any errors across 431 files (#47789) 2026-10-24 11:31:09 -07:00
unified_guardrails fix(router): preserve routing tags after guardrail metadata enrichment (#50488) 2026-11-16 11:34:48 -08:00
zscaler_ai_guard fix(zscaler_ai_guard)!: scan only the last user turn on requests (#50139) 2026-11-05 13:00:21 -08:00
__init__.py
_cisco_ai_defense_test_utils.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
code_execution_compliance_dataset.json test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_agent_365.py feat(mcp): hand listed-tool description and input schema to pre-call hooks per caller (#41162) 2026-10-04 00:53:58 -07:00
test_aim.py test: move routing, caching and callback tests out of local_testing (#45761) 2026-10-10 09:33:09 -07:00
test_airia.py feat(guardrails): add Airia guardrail integration (#40784) 2026-10-14 15:45:47 -07:00
test_alice.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_bedrock_guardrails.py fix(guardrails): log requests that a guardrail blocks on /apply_guardrail (#51014) 2026-11-16 15:18:08 -08:00
test_bedrock_invoke_guardrail_checks.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_block_code_execution.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_block_code_execution_compliance.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_cato_networks.py fix(anthropic)!: do not surface server_tool_use as client tool_calls (#48385) 2026-11-06 03:54:34 -08:00
test_cisco_ai_defense_chat.py fix(anthropic)!: do not surface server_tool_use as client tool_calls (#48385) 2026-11-06 03:54:34 -08:00
test_cisco_ai_defense_mcp.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_collieai.py feat(guardrails): add the CollieAi guardrail integration (#49224) 2026-11-12 01:05:12 +00:00
test_compresr.py fix(websearch)!: keep the provider prefix on the agentic follow-up model (#38885) 2026-10-29 04:47:18 -07:00
test_conduct.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_crowdstrike_aidr.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_custom_code_bounded_execution.py test(unit): wait on the work itself instead of fixed sleeps (#46261) 2026-10-13 12:41:06 -07:00
test_decision_model.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_deepkeep.py test: migrate SDK and proxy-booting legacy CircleCI tests to their tiers (#45758) 2026-10-11 00:17:21 -07:00
test_dynamoai.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_enkryptai.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_generic_guardrail_api.py fix(bedrock): send s3:// image, document and video URLs to Converse as S3 sources (#50577) 2026-11-13 21:33:21 -08:00
test_grayswan.py fix(guardrails): log requests that a guardrail blocks on /apply_guardrail (#51014) 2026-11-16 15:18:08 -08:00
test_headroom.py fix(headroom): forward tenant-scoped session affinity (#49255) 2026-10-30 12:32:03 -07:00
test_hiddenlayer.py fix(guardrails): avoid blocking the event loop during HiddenLayer JWT refresh (#45437) 2026-10-09 22:29:24 -07:00
test_javelin.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_lakera_ai.py fix(anthropic)!: do not surface server_tool_use as client tool_calls (#48385) 2026-11-06 03:54:34 -08:00
test_lakera_ai_v2.py refactor(typing): clear about 1,100 basedpyright Any errors across 303 files (#49986) 2026-11-06 13:31:15 -08:00
test_lasso.py fix(anthropic)!: do not surface server_tool_use as client tool_calls (#48385) 2026-11-06 03:54:34 -08:00
test_llm_shield_proxy.py fix(anthropic)!: do not surface server_tool_use as client tool_calls (#48385) 2026-11-06 03:54:34 -08:00
test_mcp_end_user_permission.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_mcp_security.py fix(mcp)!: answer 400 for a hosted MCP tool a translated route cannot run, drop it under drop_params (#48931) 2026-11-02 13:13:03 -08:00
test_microsoft_purview.py fix(responses): replay Claude tool search on follow-up turns (#50821) 2026-11-10 17:13:40 +00:00
test_model_armor.py fix(bedrock): send s3:// image, document and video URLs to Converse as S3 sources (#50577) 2026-11-13 21:33:21 -08:00
test_noma.py fix(streaming): rebuild each choice of an n>1 chat stream on its own (#49002) 2026-10-29 18:29:14 -07:00
test_noma_v2.py fix(bedrock)!: stream Converse passthrough frames through post_call guardrails instead of buffering (#46307) 2026-10-17 00:53:53 -07:00
test_onyx.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_ovalix.py feat(ovalix)!: scan files, tool calls and tool results (#49309) 2026-10-31 17:01:12 -07:00
test_ovalix_extraction.py feat(ovalix)!: scan files, tool calls and tool results (#49309) 2026-10-31 17:01:12 -07:00
test_pangea.py fix(anthropic)!: do not surface server_tool_use as client tool_calls (#48385) 2026-11-06 03:54:34 -08:00
test_panw_prisma_airs.py fix(anthropic)!: do not surface server_tool_use as client tool_calls (#48385) 2026-11-06 03:54:34 -08:00
test_presidio.py fix(guardrails): log requests that a guardrail blocks on /apply_guardrail (#51014) 2026-11-16 15:18:08 -08:00
test_presidio_union_fix.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_promptguard.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_qualifire.py fix(anthropic)!: do not surface server_tool_use as client tool_calls (#48385) 2026-11-06 03:54:34 -08:00
test_reco.py feat(guardrails): add native Reco guardrail provider (pre_call), fail closed by default (#48243) 2026-10-24 15:33:17 -07:00
test_repelloai.py fix(anthropic)!: do not surface server_tool_use as client tool_calls (#48385) 2026-11-06 03:54:34 -08:00
test_response_rejection_guardrail_code.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_singulr.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_straiker.py feat(guardrails): straiker stream_mode, per-phase logging and prompt-block replies (#51401) 2026-11-14 13:50:05 -08:00
test_structured_messages_writeback.py fix(anthropic): send an https PDF image_url to Claude as a document (#49708) 2026-11-03 07:05:15 +00:00
test_tool_permission.py fix(anthropic)!: do not surface server_tool_use as client tool_calls (#48385) 2026-11-06 03:54:34 -08:00
test_tool_policy_guardrail.py feat(proxy): mixed Codex model picker and LiteLLM routing on native codex routes (#49408) 2026-11-06 11:14:47 -08:00
test_typesafe.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_vigil_guard.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00
test_wingback.py feat(guardrails): add Wingback guardrail integration (#46136) 2026-10-12 22:18:10 -07:00
test_xecguard.py test(proxy): move management_endpoints, management_helpers and guardrails tests into tests/unit/proxy (#44003) 2026-10-01 10:52:03 -07:00