litellm/litellm/proxy/policy_engine
Yassin Kortam 4faeabc254
fix(guardrails): run pre_call hook once for model-level guardrails (#30543)
* fix(guardrails): run pre_call hook once for model-level guardrails

A CustomGuardrail attached to a deployment via litellm_params.guardrails
gets its async_pre_call_hook invoked twice per request: once by the proxy
pre-call loop and again by async_pre_call_deployment_hook after the router
spreads the model-level guardrails into the top-level request kwargs.

Record in request metadata that the proxy pre-call loop already ran a given
guardrail, and have the deployment hook skip it when the marker is present.
Direct-SDK usage never runs the proxy loop, so the deployment hook stays the
sole invocation there and still fires exactly once.

The marker key is stripped from untrusted caller metadata so a request body
cannot suppress a model-only guardrail by pre-seeding it.

* fix(guardrails): mark pre_call dedup on the post-hook request data

Record the exactly-once marker after async_pre_call_hook runs, on the data
object that flows downstream, rather than before it. A guardrail whose hook
returns a brand-new request dict (instead of mutating or spreading the one it
received) would otherwise discard the marker, letting the deployment hook
re-run the guardrail a second time.
2026-06-16 11:17:03 -07:00
..
__init__.py [Feat] New LiteLLM Policy engine - create policies to manage guardrails, conditions - permissions per Key, Team (#19612) 2026-01-22 19:49:53 -08:00
architecture.md [Feat] New LiteLLM Policy engine - create policies to manage guardrails, conditions - permissions per Key, Team (#19612) 2026-01-22 19:49:53 -08:00
attachment_registry.py feat(litellm): add models and repository layers (#29686) 2026-06-06 20:59:33 -07:00
condition_evaluator.py [Feat] New LiteLLM Policy engine - create policies to manage guardrails, conditions - permissions per Key, Team (#19612) 2026-01-22 19:49:53 -08:00
init_policies.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
pipeline_executor.py fix(guardrails): run pre_call hook once for model-level guardrails (#30543) 2026-06-16 11:17:03 -07:00
policy_endpoints.py style: run black formatter on entire codebase 2026-03-11 17:07:57 -03:00
policy_matcher.py style: run black formatter on entire codebase 2026-03-11 17:07:57 -03:00
policy_registry.py feat(litellm): add models and repository layers (#29686) 2026-06-06 20:59:33 -07:00
policy_resolve_endpoints.py feat(litellm): add models and repository layers (#29686) 2026-06-06 20:59:33 -07:00
policy_resolver.py style: run black formatter on entire codebase 2026-03-11 17:07:57 -03:00
policy_validator.py feat(litellm): add models and repository layers (#29686) 2026-06-06 20:59:33 -07:00