Guardrails
GuardrailFunctionOutputdataclass
The output of a guardrail function.
Source code insrc/agents/guardrail.py
output_infoinstance-attribute
Optional information about the guardrail's output. For example, the guardrail could includeinformation about the checks it performed and granular results.
InputGuardrailResultdataclass
The result of a guardrail run.
Source code insrc/agents/guardrail.py
OutputGuardrailResultdataclass
The result of a guardrail run.
Source code insrc/agents/guardrail.py
agent_outputinstance-attribute
The output of the agent that was checked by the guardrail.
InputGuardraildataclass
Bases:Generic[TContext]
Input guardrails are checks that run either in parallel with the agent or before it starts.They can be used to do things like:- Check if input messages are off-topic- Take over control of the agent's execution if an unexpected input is detected
You can use the@input_guardrail() decorator to turn a function into anInputGuardrail, orcreate anInputGuardrail manually.
Guardrails return aGuardrailResult. Ifresult.tripwire_triggered isTrue,the agent's execution will immediately stop, andanInputGuardrailTripwireTriggered exception will be raised
Source code insrc/agents/guardrail.py
guardrail_functioninstance-attribute
guardrail_function:Callable[[RunContextWrapper[TContext],Agent[Any],str|list[TResponseInputItem],],MaybeAwaitable[GuardrailFunctionOutput],]A function that receives the agent input and the context, and returns aGuardrailResult. The result marks whether the tripwire was triggered, and can optionallyinclude information about the guardrail's output.
nameclass-attributeinstance-attribute
The name of the guardrail, used for tracing. If not provided, we'll use the guardrailfunction's name.
OutputGuardraildataclass
Bases:Generic[TContext]
Output guardrails are checks that run on the final output of an agent.They can be used to do check if the output passes certain validation criteria
You can use the@output_guardrail() decorator to turn a function into anOutputGuardrail,or create anOutputGuardrail manually.
Guardrails return aGuardrailResult. Ifresult.tripwire_triggered isTrue, anOutputGuardrailTripwireTriggered exception will be raised.
Source code insrc/agents/guardrail.py
guardrail_functioninstance-attribute
guardrail_function:Callable[[RunContextWrapper[TContext],Agent[Any],Any],MaybeAwaitable[GuardrailFunctionOutput],]A function that receives the final agent, its output, and the context, and returns aGuardrailResult. The result marks whether the tripwire was triggered, and can optionallyinclude information about the guardrail's output.
input_guardrail
input_guardrail(func:_InputGuardrailFuncSync[TContext_co],)->InputGuardrail[TContext_co]input_guardrail(func:_InputGuardrailFuncAsync[TContext_co],)->InputGuardrail[TContext_co]input_guardrail(*,name:str|None=None,run_in_parallel:bool=True)->Callable[[_InputGuardrailFuncSync[TContext_co]|_InputGuardrailFuncAsync[TContext_co]],InputGuardrail[TContext_co],]input_guardrail(func:_InputGuardrailFuncSync[TContext_co]|_InputGuardrailFuncAsync[TContext_co]|None=None,*,name:str|None=None,run_in_parallel:bool=True,)->(InputGuardrail[TContext_co]|Callable[[_InputGuardrailFuncSync[TContext_co]|_InputGuardrailFuncAsync[TContext_co]],InputGuardrail[TContext_co],])Decorator that transforms a sync or async function into anInputGuardrail.It can be used directly (no parentheses) or with keyword args, e.g.:
@input_guardraildef my_sync_guardrail(...): ...@input_guardrail(name="guardrail_name", run_in_parallel=False)async def my_async_guardrail(...): ...Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func | _InputGuardrailFuncSync[TContext_co] |_InputGuardrailFuncAsync[TContext_co] | None | The guardrail function to wrap. | None |
name | str | None | Optional name for the guardrail. If not provided, uses the function's name. | None |
run_in_parallel | bool | Whether to run the guardrail concurrently with the agent (True, default)or before the agent starts (False). | True |
Source code insrc/agents/guardrail.py
output_guardrail
output_guardrail(func:_OutputGuardrailFuncSync[TContext_co],)->OutputGuardrail[TContext_co]output_guardrail(func:_OutputGuardrailFuncAsync[TContext_co],)->OutputGuardrail[TContext_co]output_guardrail(*,name:str|None=None)->Callable[[_OutputGuardrailFuncSync[TContext_co]|_OutputGuardrailFuncAsync[TContext_co]],OutputGuardrail[TContext_co],]output_guardrail(func:_OutputGuardrailFuncSync[TContext_co]|_OutputGuardrailFuncAsync[TContext_co]|None=None,*,name:str|None=None,)->(OutputGuardrail[TContext_co]|Callable[[_OutputGuardrailFuncSync[TContext_co]|_OutputGuardrailFuncAsync[TContext_co]],OutputGuardrail[TContext_co],])Decorator that transforms a sync or async function into anOutputGuardrail.It can be used directly (no parentheses) or with keyword args, e.g.:
@output_guardraildef my_sync_guardrail(...): ...@output_guardrail(name="guardrail_name")async def my_async_guardrail(...): ...