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 Rules

From Leeroopedia
Revision as of 12:11, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/BerriAI_Litellm_Rules.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Attribute Value
Sources litellm/litellm_core_utils/rules.py
Domains Guardrails, Validation, Pre/Post-Call Rules
Last Updated 2026-02-15 16:00 GMT

Overview

Implements a rule engine that evaluates user-defined pre-call and post-call rules against LLM inputs and outputs, triggering fallback behavior on rule failures.

Description

The Rules class provides a mechanism to enforce custom validation rules on LLM call inputs (pre-call) and outputs (post-call). Rules are registered as callable functions on the global litellm.pre_call_rules and litellm.post_call_rules lists.

  • Pre-call rules receive the input string and must return True (pass) or False (fail).
  • Post-call rules receive the model response text and can return either a boolean or a dictionary with decision and optional message keys.

When a rule returns False, the class raises litellm.APIResponseValidationError, which triggers the fallback mechanism when fallback models are configured. The static method has_pre_call_rules allows callers to check whether any pre-call rules are registered before incurring the overhead of rule evaluation.

Usage

Import Rules and instantiate it within the completion pipeline to enforce validation. Register rule functions on litellm.pre_call_rules or litellm.post_call_rules before making calls.

Code Reference

Source Location

litellm/litellm_core_utils/rules.py (55 lines)

Signature

class Rules:
    def __init__(self) -> None

    @staticmethod
    def has_pre_call_rules() -> bool

    def pre_call_rules(self, input: str, model: str) -> bool

    def post_call_rules(self, input: Optional[str], model: str) -> bool

Import

from litellm.litellm_core_utils.rules import Rules

I/O Contract

has_pre_call_rules

Direction Name Type Description
Output return bool True if litellm.pre_call_rules is non-empty

pre_call_rules

Direction Name Type Description
Input input str The user input text to validate
Input model str The model name being called
Output return bool True if all rules pass
Output raises APIResponseValidationError If any rule returns False

post_call_rules

Direction Name Type Description
Input input Optional[str] The model response text to validate
Input model str The model name that generated the response
Output return bool True if all rules pass
Output raises APIResponseValidationError If any rule returns False (or {"decision": False})

Usage Examples

import litellm
from litellm.litellm_core_utils.rules import Rules

# Define a post-call rule that rejects refusals
def reject_refusals(response_text):
    if "i don't think i can answer" in response_text.lower():
        return False
    return True

# Register the rule
litellm.post_call_rules = [reject_refusals]

# Use with fallbacks -- if the rule fails, the fallback model is tried
response = litellm.completion(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Hey, how's it going?"}],
    fallbacks=["openrouter/mythomax"],
)

# Define a rule returning a dict with a custom message
def check_safety(response_text):
    if "unsafe content" in response_text:
        return {"decision": False, "message": "Response contains unsafe content"}
    return {"decision": True}

litellm.post_call_rules = [check_safety]

# Check if pre-call rules are configured
if Rules.has_pre_call_rules():
    rules = Rules()
    rules.pre_call_rules(input="test input", model="gpt-4")

Related Pages

Page Connections

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