- Return
None(or nothing) — pass through unchanged. - Return a value (
str,dict, or the appropriate type) replace or update the input/output. - Raise an exception block execution entirely.
asyncio.to_thread.
Guardrails declare only the parameters they care about. The system inspects the function signature and extracts matching values automatically — you never need to accept the full set of arguments.
Input guardrails
Tool input guardrails run before the tool function executes. They receive keyword arguments that match their declared parameter names.query — other tool parameters are ignored.
To modify an argument instead of blocking, return a dict. The returned dict merges into the tool’s kwargs — keys you omit stay unchanged:
Output guardrails
Tool output guardrails run after the tool returns, before the result is encoded back to the model. They receive the typed return value directly:Agent guardrails
Attach guardrails to aReActAgent using input_guardrails and output_guardrails:
- Input guardrails receive the user’s prompt as a string. If the guardrail also declares an
agentparameter, the agent instance is passed in. Return a string to rewrite the prompt; raiseInputGuardrailTrippedto block the run. - Output guardrails receive the final response string (when no
response_formatis set). Return a string to replace the output; raiseOutputGuardrailTrippedto block.
Tool guardrails (decorator)
Attach guardrails directly on the@tool decorator:
" Hello WORLD " flows through: normalize → lowercase → profanity check. The tool receives "hello world".
Agent-level guardrails also support
parallel=True mode, where all guardrails run concurrently on the original value. Modifications are discarded — only tripwire exceptions take effect.Structured output guardrails
When the agent usesresponse_format with a Pydantic BaseModel, output guardrails use field matching — declare only the fields you need to inspect:
validate_score declares score — other fields are passed through untouched. Return a dict for partial updates, for example {"raw_data": "[REDACTED]"}.
Where to attach guardrails
| Level | How | Parameters |
|---|---|---|
| Single tool | @tool(...) or FunctionTool(...) | input_guardrails, output_guardrails |
| Tool collection | @tools(...) on the class | input_guardrails, output_guardrails |
| Agent | ReActAgent(...) | input_guardrails, output_guardrails |
@tool guardrails override class-level defaults — they do not merge.
Exception reference
All guardrail exceptions inherit fromGuardrailTripped:
| Exception | Where it applies |
|---|---|
InputGuardrailTripped | Agent input guardrails |
OutputGuardrailTripped | Agent output guardrails |
ToolInputGuardrailTripped | Tool input guardrails |
ToolOutputGuardrailTripped | Tool output guardrails |

