Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Guardrails ai Guardrails Guard Validator Composition

From Leeroopedia
Knowledge Sources
Domains Validation, Design_Pattern
Last Updated 2026-02-14 00:00 GMT

Overview

A composition pattern that assembles multiple independent validators into a single Guard pipeline through a fluent builder interface.

Description

Guard Validator Composition is the principle of combining multiple validation rules into a cohesive validation pipeline. Each validator is an independent unit that checks a specific aspect of LLM output (format, content safety, factual accuracy, etc.). The Guard acts as a composite that orchestrates these validators, routing each one to the appropriate part of the output (full output, specific JSON fields, or input messages).

This follows the Decorator/Chain of Responsibility pattern: validators are attached to a Guard instance via a fluent API (guard.use(validator)), and each validator specifies its target (output, messages, or a JSON path) and its failure action (exception, fix, filter, reask, etc.).

Usage

Apply this principle when constructing validation pipelines for LLM output. Use Guard.use() for adding validators one at a time, or Guard.use_many() for batch attachment. Choose the on parameter to target validators to specific parts of the output.

Theoretical Basis

The composition model operates on three axes:

  1. Validator Selection: Choose validators based on the output requirements (format, safety, quality)
  2. Target Routing: Assign each validator to a target: "output" (full LLM response), "messages" (input validation), or a JSON path like "$.field" (field-level validation)
  3. Failure Strategy: Each validator specifies an OnFailAction that determines what happens when validation fails:
    • EXCEPTION - raise an error
    • REASK - ask the LLM to regenerate
    • FIX - apply automatic correction
    • FILTER - remove the failing element
    • REFRAIN - return None for the entire output
    • NOOP - log but continue

Related Pages

Implemented By

Page Connections

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