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:- Callable - Function? Use as validator
- Config - GuardrailConfig instance? Use as-is
- Array -
["preset", {"override": value}]? Apply overrides - Dict -
{"key": value}? Convert to config - String - Preset name or LLM prompt? Look up or use as prompt
- 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
- Keep guardrails focused - Each guardrail should check one specific aspect
- Provide clear error messages - Help users understand why validation failed
- Use LLM guardrails for complex validation - When rules are hard to express programmatically
- Chain guardrails for multiple checks - Combine simple guardrails for comprehensive validation
- Handle errors gracefully - Guardrails should not crash the application

