Implementation:BerriAI Litellm Enterprise Custom Guardrail
| Attribute | Value |
|---|---|
| Sources | enterprise/litellm_enterprise/integrations/custom_guardrail.py |
| Domains | Guardrails, Tag-Based Routing, Enterprise |
| Last Updated | 2026-02-15 16:00 GMT |
Overview
EnterpriseCustomGuardrailHelper provides enterprise-only logic to determine whether a guardrail should run based on request tags and guardrail Mode configuration.
Description
This class contains a single static method, _should_run_if_mode_by_tag, which implements tag-based guardrail execution control. This is an enterprise-only feature that enables fine-grained control over which requests trigger which guardrails based on request tags.
How It Works:
- The method checks if the
event_hookis an instance ofMode(as opposed to a simpleGuardrailEventHooksenum). If not, it returnsNone(no tag-based decision). - It extracts request tags from
litellm_paramsandproxy_server_requestusingStandardLoggingPayloadSetup._get_request_tags. - It checks if any request tag matches the guardrail's
Mode.tagslist. - If no tag matches, it checks if any request tag matches the
Mode.defaultlist as a fallback. - Returns
Trueif a match is found,Falseotherwise.
A premium user license is required; an exception is raised if the user is not premium.
Usage
This helper is called internally by the CustomGuardrail.should_run_guardrail method when a guardrail's event hook is configured with Mode (tag-based) instead of simple event hooks.
Code Reference
Source Location
enterprise/litellm_enterprise/integrations/custom_guardrail.py
Signature
class EnterpriseCustomGuardrailHelper:
@staticmethod
def _should_run_if_mode_by_tag(
data: dict,
event_hook: Optional[Union[GuardrailEventHooks, List[GuardrailEventHooks], Mode]],
) -> Optional[bool]: ...
Import
from litellm_enterprise.integrations.custom_guardrail import EnterpriseCustomGuardrailHelper
I/O Contract
Inputs
| Parameter | Type | Description |
|---|---|---|
data |
dict |
Request data containing proxy_server_request and litellm params.
|
event_hook |
Optional[Union[GuardrailEventHooks, List[GuardrailEventHooks], Mode]] |
The guardrail's event hook configuration. Must be Mode for tag-based evaluation.
|
Outputs
| Output | Type | Description |
|---|---|---|
| Tag match result | Optional[bool] |
True if a request tag matches; False if no match; None if event_hook is not a Mode instance.
|
Usage Examples
# Guardrail config with tag-based mode
guardrails:
- guardrail_name: "my-guardrail"
litellm_params:
guardrail: aporia
mode:
tags: ["production", "high-risk"]
default: ["standard"]
# Request with tags that trigger the guardrail
response = litellm.completion(
model="gpt-4",
messages=[{"role": "user", "content": "Hello"}],
metadata={"tags": ["production"]},
)
Related Pages
- Implementation:BerriAI_Litellm_Aporia_AI_Guardrail -- Aporia AI guardrail that uses this helper
- Implementation:BerriAI_Litellm_LLM_Guard -- LLM Guard integration
- Implementation:BerriAI_Litellm_Secret_Detection -- Secret detection guardrail