Choose the Right Guardrail
| Type | Runs When | On Failure | Page |
|---|---|---|---|
| Input Guardrails | Before the agent processes input | Return guidance (non-strict) or raise InputGuardrailTripwireTriggered (strict) | Input Guardrails |
| Output Guardrails | After the agent drafts output, before delivery | Retry up to validation_attempts, then raise OutputGuardrailTripwireTriggered | Output Guardrails |
Runnable GitHub Examples
- Input guardrails example:
examples/guardrails_input.py - Output guardrails example:
examples/guardrails_output.py
Best Practices
- Keep each guardrail focused on one responsibility.
- Write clear
output_infomessages so the model knows how to recover. - Use non-strict mode for guidance and strict mode for hard requirements.
- For tool argument validation (field/model validators), use Input Validation.