Implementation:Evidentlyai Evidently Guardrails Core
| 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 aname()method (defaults to the class name) and an abstractvalidate(data)method that raisesGuardExceptionon 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. Itsvalidateis a no-op.GuardsException-- A specializedGuardExceptionthat aggregates multiple failures into a single exception. Contains a dictionary mapping each failedGuardrailBaseinstance to its correspondingGuardException.validate_guards-- Utility function that iterates over a list of guards, callsvalidateon each, collects any failures, and raises aGuardsExceptionif 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
- Repository: Evidentlyai_Evidently
- File:
src/evidently/guardrails/core.py
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)