Quick Start
Which Validator Should I Use?
Pick a validator strategy based on how you need to check the output.How It Works
Guardrails work identically in sync (.start(), .chat()) and async (.astart(), .achat()) execution paths.
Configuration Options
GuardrailConfig SDK Reference
Full parameter reference for GuardrailConfig
| Option | Type | Default | Description |
|---|---|---|---|
validator | Callable | None | None | Function (output) -> (bool, Any) for programmatic validation |
llm_validator | str | None | None | Natural language criteria for LLM-based validation |
max_retries | int | 3 | Maximum retry attempts on validation failure |
on_fail | str | "retry" | Action on failure: "retry", "skip", or "raise" |
policies | List[str] | [] | Policy strings like ["policy:strict", "pii:redact"] |
Common Patterns
Function-Based Validation
Natural Language Validation
Multi-Agent with Guardrails
Best Practices
Write specific, measurable criteria
Write specific, measurable criteria
Vague guardrails like “be good” are hard to enforce. Use concrete criteria: “must be between 100 and 200 words” or “must contain a JSON array”.
Use function validators for structured data
Use function validators for structured data
When validating JSON, code, or data formats, use a function validator. LLM validators are slower and better suited for qualitative criteria like tone or completeness.
Return helpful error messages on failure
Return helpful error messages on failure
The
(False, "reason") message is passed back to the agent as feedback. Make it actionable — tell the agent exactly what to fix.Set max_retries conservatively
Set max_retries conservatively
Start with
max_retries=2. Increasing retries adds latency and cost. If the agent fails repeatedly, the validator criteria or instructions may need refinement.Related
Approval
Add human-in-the-loop approval steps
Hooks
Intercept and modify agent behavior at lifecycle points
Gateway Self-Lifecycle Guard
Block agent commands that would stop or restart this gateway

