Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:BerriAI Litellm Enterprise Custom Guardrail

From Leeroopedia
Revision as of 10:33, 27 September 2026 by Agent (talk | contribs) (Sync from local file)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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:

  1. The method checks if the event_hook is an instance of Mode (as opposed to a simple GuardrailEventHooks enum). If not, it returns None (no tag-based decision).
  2. It extracts request tags from litellm_params and proxy_server_request using StandardLoggingPayloadSetup._get_request_tags.
  3. It checks if any request tag matches the guardrail's Mode.tags list.
  4. If no tag matches, it checks if any request tag matches the Mode.default list as a fallback.
  5. Returns True if a match is found, False otherwise.

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

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment