Skip to main content

Guardrail Configuration

This page provides comprehensive documentation for configuring guardrails in PraisonAI, including custom validation rules, safety checks, content filtering, and compliance enforcement.

Guardrail System Overview

Guardrails ensure AI agents operate safely, ethically, and within defined boundaries. They provide multiple layers of protection:
  • Input Validation: Validate and sanitize inputs before processing
  • Output Filtering: Ensure outputs meet quality and safety standards
  • Behavior Control: Prevent unwanted agent behaviors
  • Compliance Enforcement: Ensure regulatory compliance

Basic Guardrail Configuration

Guardrail Presets (Agent-Centric API)

Since PraisonAI PR #6060, on_fail only accepts "retry" (the default). on_fail="raise", on_fail="skip", or any other value raises ValueError at Agent(...) construction.
Presets set the retry budget. Pair a preset with a validator in list form:
A preset with no validator (guardrails=["strict"]) only sets max_retries — nothing is enforced.

Preset with Overrides

Policy Strings (removed)

Since PraisonAI PR #3790, policy-string guardrails such as guardrails=["policy:strict", "pii:redact"] raise ValueError at Agent(...) construction time. For tool allow/deny enforcement, use Agent(policy=PolicyEngine(...)) instead — see Policy Engine.

LLM-Based Validation

A bare string is always an LLM-validation prompt — never a preset lookup. guardrails="strict" validates against the literal word “strict”, so write real criteria:

GuardrailConfig for Full Control

Observing Blocked Responses

When retries run out, the agent returns None and stores the last rejection reason on agent.last_guardrail_error. Use this in place of the old on_fail="raise":

Custom Validation Rules

Rule Types and Configuration

Custom Validation Functions

Advanced Guardrail Patterns

Layered Guardrails

Conditional Guardrails

Dynamic Guardrails

Compliance and Regulatory Guardrails

GDPR Compliance

Financial Compliance

Performance and Optimization

Guardrail Performance Configuration

Complete Guardrail Configuration Example

Environment Variables

Best Practices

  1. Layer your guardrails - Use multiple layers for defense in depth
  2. Fail fast on critical violations - Don’t waste resources on invalid requests
  3. Cache validation results - Improve performance for repeated checks
  4. Monitor guardrail performance - Ensure guardrails don’t become bottlenecks
  5. Use appropriate enforcement levels - Balance security with usability
  6. Implement graceful degradation - Have fallback behaviors for guardrail failures
  7. Regular rule updates - Keep validation rules current with threats
  8. Comprehensive logging - Maintain audit trails for compliance

See Also