litellm/tests/unit/proxy/guardrails
Agung Besti d7ab7255e2
feat(guardrails): scan an image on /apply_guardrail with a Bedrock guardrail (#51016)
* 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.

* feat(guardrails): scan an image on /apply_guardrail with a Bedrock guardrail

/apply_guardrail only took text. It now takes `images`, one base64 data URI per request, and `text` becomes optional. The image is validated with the attachment reader the Bedrock guardrail already uses for chat completions and sent to ApplyGuardrail through the same call, with source OUTPUT for a response-direction scan. The image is scanned first, and the text scan is skipped only when the request has neither text nor messages.

A request the guardrail cannot judge answers 422 instead of 200 or 400. That covers an unreadable image, a guardrail that is not a Bedrock ApplyGuardrail guardrail, and an answer in which Bedrock does not report the image as guarded.

* test(guardrails): run the image tests through the endpoint's real request setup

The endpoint test helper on main now goes through the real request setup and guardrail lookup, and expects each request to leave one log entry. A body the request model refuses never reaches the endpoint, so those two cases are checked on the model. The guardrail that cannot scan images now carries the name the request asks for.

* fix(guardrails): log an image Bedrock did not evaluate as not run

An ApplyGuardrail image call that Bedrock answered without guarding the image
was logged as a guardrail success, so the request log read as a pass for an
image nobody checked. It now logs as not_run. A Bedrock error inside a 200
body on /apply_guardrail answers 502 naming the AWS error type instead of the
Image modality message

* fix(guardrails): refuse message text a Bedrock guardrail would not scan

* fix(guardrails): answer 422 when Bedrock cannot evaluate an image on /apply_guardrail

* refactor(guardrails): return image scan failures as values and type the test override

* fix(guardrails): keep Bedrock API errors reporting as HTTPException and log image-only prompts

* fix(guardrails): keep the scanned image in the apply_guardrail spend log request

* fix(guardrails): redact the apply_guardrail image when message logging is off and keep Bedrock API errors a plain HTTPException

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-17 18:13:28 +00:00
..
guardrail_hooks feat(guardrails): scan an image on /apply_guardrail with a Bedrock guardrail (#51016) 2026-11-17 18:13:28 +00:00
__init__.py
test_anthropic_sse.py fix(guardrails)!: keep the upstream error when a /v1/messages stream ends in one (#49088) 2026-10-31 02:56:20 -07:00
test_auto_router_compression.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_content_filter_path_traversal.py
test_content_utils.py fix(guardrails): skip embeddings input in google_text_moderation (#48934) 2026-10-28 20:05:03 -07:00
test_custom_code_security.py
test_deferred_guardrail_logging.py fix(logging): retain streaming success logging tasks until they finish (#52067) 2026-11-16 11:21:05 -08:00
test_endpoint_image_scan.py feat(guardrails): scan an image on /apply_guardrail with a Bedrock guardrail (#51016) 2026-11-17 18:13:28 +00:00
test_guardrail_coverage.py chore: move workspace packages under litellm (#51000) 2026-11-12 04:19:57 +00:00
test_guardrail_endpoints.py feat(guardrails): scan an image on /apply_guardrail with a Bedrock guardrail (#51016) 2026-11-17 18:13:28 +00:00
test_guardrail_helpers.py fix(proxy): reject unknown guardrail names on key, user, team and project writes (#51080) 2026-11-11 19:28:45 -08:00
test_guardrail_initializers.py refactor(types): replace Any with proven types in 157 files (#44798) 2026-10-06 15:31:54 +00:00
test_guardrail_load_balancing.py test: migrate SDK and proxy-booting legacy CircleCI tests to their tiers (#45758) 2026-10-11 00:17:21 -07:00
test_guardrail_registry.py feat(guardrails): add the CollieAi guardrail integration (#49224) 2026-11-12 01:05:12 +00:00
test_init_guardrails.py fix(config): treat blank env values as unset so the proxy neither crash-loops nor silently drops a guardrail (#46341) 2026-10-14 12:49:31 -07:00
test_llm_as_a_judge.py
test_mcp_jwt_signer.py fix(mcp)!: forward the LiteLLM key when the admin configures it (#50604) 2026-11-10 20:51:34 -08:00
test_pillar_guardrails.py refactor(typing): clear about 1,100 basedpyright Any errors across 303 files (#49986) 2026-11-06 13:31:15 -08:00
test_prompt_security_guardrails.py fix(anthropic)!: send /v1/messages PDF documents to OpenAI-shaped providers as file parts (#47166) 2026-10-29 08:06:12 -07:00
test_qostodian_nexus_guardrail.py
test_usage_endpoints.py
test_usage_tracking.py feat(accounting): add opt-in SpendLogs ledger (#49856) 2026-11-04 17:49:15 -08:00