Skip to main content

Guardrails

Validation and safety mechanisms for agent outputs. Supports function-based, LLM-based, and preset guardrails.

Quick Start

Feature Usage Table

Presets & Options

Precedence Ladder

Resolution Order: Instance > Config > Array > Dict > String > Bool > DefaultWhen you pass guardrails=, the resolver checks in this order:
  1. Callable - Function? Use as validator
  2. Config - GuardrailConfig instance? Use as-is
  3. Array - ["preset", {"override": value}]? Apply overrides
  4. Dict - {"key": value}? Convert to config
  5. String - Preset name or LLM prompt? Look up or use as prompt
  6. Bool - True? Use defaults. False? Disable

Usage Forms

String Preset

Array with Overrides

LLM Prompt (String)

Callable Function

Config Instance

Classes

GuardrailResult

Result of a guardrail validation.

LLMGuardrail

LLM-powered guardrail using natural language.

Common Recipes

Function-based Guardrail

LLM-based Guardrail

Multiple Guardrails

Best Practices

  1. Keep guardrails focused - Each guardrail should check one specific aspect
  2. Provide clear error messages - Help users understand why validation failed
  3. Use LLM guardrails for complex validation - When rules are hard to express programmatically
  4. Chain guardrails for multiple checks - Combine simple guardrails for comprehensive validation
  5. Handle errors gracefully - Guardrails should not crash the application
  • Task - Task configuration with guardrails
  • Agent - Agent configuration