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:Evidentlyai Evidently Guardrails Core

From Leeroopedia
Revision as of 12:27, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Evidentlyai_Evidently_Guardrails_Core.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Guardrails, Validation, LLM_Safety
Last Updated 2026-02-14 12:00 GMT

Overview

guardrails/core.py defines the base guardrail abstraction, exception types, and a utility function for validating input data against a list of guardrails.

Description

This module provides the foundational classes for the Evidently guardrails subsystem:

  • GuardrailBase -- Abstract base class for all guardrails. Each guardrail has a name() method (defaults to the class name) and an abstract validate(data) method that raises GuardException on failure.
  • GuardException -- Exception raised when a single guardrail validation fails. Carries a reference to the failing guard and an error message.
  • AggregationGuardrail -- A concrete guardrail subclass used as a sentinel/placeholder guard in aggregated exception contexts. Its validate is a no-op.
  • GuardsException -- A specialized GuardException that aggregates multiple failures into a single exception. Contains a dictionary mapping each failed GuardrailBase instance to its corresponding GuardException.
  • validate_guards -- Utility function that iterates over a list of guards, calls validate on each, collects any failures, and raises a GuardsException if one or more guards fail.

The design allows guardrails to be composed and validated in batch, with aggregated error reporting.

Usage

Use these classes when building custom guardrails for LLM pipelines or data validation. Subclass GuardrailBase to implement custom validation logic, then use validate_guards to run all guards against input data.

Code Reference

Source Location

Signature

class GuardrailBase:
    def __init__(self): ...
    def name(self) -> str: ...
    @abc.abstractmethod
    def validate(self, data: str): ...

class GuardException(Exception):
    guard: GuardrailBase
    def __init__(self, guard: GuardrailBase, message: str = "") -> None: ...

class AggregationGuardrail(GuardrailBase):
    def validate(self, data: str): ...
    def name(self) -> str: ...

class GuardsException(GuardException):
    failed_guards: Dict[GuardrailBase, GuardException]
    def __init__(self, failed_guards: Dict[GuardrailBase, GuardException]): ...

def validate_guards(data: str, guards: List[GuardrailBase]): ...

Import

from evidently.guardrails.core import GuardrailBase, GuardException, GuardsException, validate_guards

I/O Contract

Inputs

Name Type Required Description
data str Yes The input data string to validate against guardrail criteria.
guards List[GuardrailBase] Yes List of guardrail instances to run validation against (for validate_guards).

Outputs

Name Type Description
validate_guards return None Returns nothing on success; raises GuardsException if any guard fails.
GuardException Exception Raised when a single guard validation fails.
GuardsException GuardException Raised when one or more guards fail, containing all failures in failed_guards.

Usage Examples

from evidently.guardrails.core import GuardrailBase, GuardException, validate_guards

class MaxLengthGuard(GuardrailBase):
    def __init__(self, max_length: int):
        super().__init__()
        self.max_length = max_length

    def validate(self, data: str):
        if len(data) > self.max_length:
            raise GuardException(self, f"Input exceeds max length {self.max_length}")

# Validate input against multiple guards
guards = [MaxLengthGuard(1000)]
validate_guards("Some user input text", guards)

Related Pages

Page Connections

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