Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Guardrails

GuardrailFunctionOutputdataclass

The output of a guardrail function.

Source code insrc/agents/guardrail.py
@dataclassclassGuardrailFunctionOutput:"""The output of a guardrail function."""output_info:Any"""    Optional information about the guardrail's output. For example, the guardrail could include    information about the checks it performed and granular results.    """tripwire_triggered:bool"""    Whether the tripwire was triggered. If triggered, the agent's execution will be halted.    """

output_infoinstance-attribute

output_info:Any

Optional information about the guardrail's output. For example, the guardrail could includeinformation about the checks it performed and granular results.

tripwire_triggeredinstance-attribute

tripwire_triggered:bool

Whether the tripwire was triggered. If triggered, the agent's execution will be halted.

InputGuardrailResultdataclass

The result of a guardrail run.

Source code insrc/agents/guardrail.py
@dataclassclassInputGuardrailResult:"""The result of a guardrail run."""guardrail:InputGuardrail[Any]"""    The guardrail that was run.    """output:GuardrailFunctionOutput"""The output of the guardrail function."""

guardrailinstance-attribute

guardrail:InputGuardrail[Any]

The guardrail that was run.

outputinstance-attribute

The output of the guardrail function.

OutputGuardrailResultdataclass

The result of a guardrail run.

Source code insrc/agents/guardrail.py
@dataclassclassOutputGuardrailResult:"""The result of a guardrail run."""guardrail:OutputGuardrail[Any]"""    The guardrail that was run.    """agent_output:Any"""    The output of the agent that was checked by the guardrail.    """agent:Agent[Any]"""    The agent that was checked by the guardrail.    """output:GuardrailFunctionOutput"""The output of the guardrail function."""

guardrailinstance-attribute

guardrail:OutputGuardrail[Any]

The guardrail that was run.

agent_outputinstance-attribute

agent_output:Any

The output of the agent that was checked by the guardrail.

agentinstance-attribute

agent:Agent[Any]

The agent that was checked by the guardrail.

outputinstance-attribute

The output of the guardrail function.

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
@dataclassclassInputGuardrail(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 an `InputGuardrail`, or    create an `InputGuardrail` manually.    Guardrails return a `GuardrailResult`. If `result.tripwire_triggered` is `True`,    the agent's execution will immediately stop, and    an `InputGuardrailTripwireTriggered` exception will be raised    """guardrail_function:Callable[[RunContextWrapper[TContext],Agent[Any],str|list[TResponseInputItem]],MaybeAwaitable[GuardrailFunctionOutput],]"""A function that receives the agent input and the context, and returns a     `GuardrailResult`. The result marks whether the tripwire was triggered, and can optionally     include information about the guardrail's output.    """name:str|None=None"""The name of the guardrail, used for tracing. If not provided, we'll use the guardrail    function's name.    """run_in_parallel:bool=True"""Whether the guardrail runs concurrently with the agent (True, default) or before    the agent starts (False).    """defget_name(self)->str:ifself.name:returnself.namereturnself.guardrail_function.__name__asyncdefrun(self,agent:Agent[Any],input:str|list[TResponseInputItem],context:RunContextWrapper[TContext],)->InputGuardrailResult:ifnotcallable(self.guardrail_function):raiseUserError(f"Guardrail function must be callable, got{self.guardrail_function}")output=self.guardrail_function(context,agent,input)ifinspect.isawaitable(output):returnInputGuardrailResult(guardrail=self,output=awaitoutput,)returnInputGuardrailResult(guardrail=self,output=output,)

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

name:str|None=None

The name of the guardrail, used for tracing. If not provided, we'll use the guardrailfunction's name.

run_in_parallelclass-attributeinstance-attribute

run_in_parallel:bool=True

Whether the guardrail runs concurrently with the agent (True, default) or beforethe agent starts (False).

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
@dataclassclassOutputGuardrail(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 an `OutputGuardrail`,    or create an `OutputGuardrail` manually.    Guardrails return a `GuardrailResult`. If `result.tripwire_triggered` is `True`, an    `OutputGuardrailTripwireTriggered` exception will be raised.    """guardrail_function:Callable[[RunContextWrapper[TContext],Agent[Any],Any],MaybeAwaitable[GuardrailFunctionOutput],]"""A function that receives the final agent, its output, and the context, and returns a     `GuardrailResult`. The result marks whether the tripwire was triggered, and can optionally     include information about the guardrail's output.    """name:str|None=None"""The name of the guardrail, used for tracing. If not provided, we'll use the guardrail    function's name.    """defget_name(self)->str:ifself.name:returnself.namereturnself.guardrail_function.__name__asyncdefrun(self,context:RunContextWrapper[TContext],agent:Agent[Any],agent_output:Any)->OutputGuardrailResult:ifnotcallable(self.guardrail_function):raiseUserError(f"Guardrail function must be callable, got{self.guardrail_function}")output=self.guardrail_function(context,agent,agent_output)ifinspect.isawaitable(output):returnOutputGuardrailResult(guardrail=self,agent=agent,agent_output=agent_output,output=awaitoutput,)returnOutputGuardrailResult(guardrail=self,agent=agent,agent_output=agent_output,output=output,)

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.

nameclass-attributeinstance-attribute

name:str|None=None

The name of the guardrail, used for tracing. If not provided, we'll use the guardrailfunction's name.

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:

NameTypeDescriptionDefault
func_InputGuardrailFuncSync[TContext_co] |_InputGuardrailFuncAsync[TContext_co] | None

The guardrail function to wrap.

None
namestr | None

Optional name for the guardrail. If not provided, uses the function's name.

None
run_in_parallelbool

Whether to run the guardrail concurrently with the agent (True, default)or before the agent starts (False).

True
Source code insrc/agents/guardrail.py
definput_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 an `InputGuardrail`.    It can be used directly (no parentheses) or with keyword args, e.g.:        @input_guardrail        def my_sync_guardrail(...): ...        @input_guardrail(name="guardrail_name", run_in_parallel=False)        async def my_async_guardrail(...): ...    Args:        func: The guardrail function to wrap.        name: Optional name for the guardrail. If not provided, uses the function's name.        run_in_parallel: Whether to run the guardrail concurrently with the agent (True, default)            or before the agent starts (False).    """defdecorator(f:_InputGuardrailFuncSync[TContext_co]|_InputGuardrailFuncAsync[TContext_co],)->InputGuardrail[TContext_co]:returnInputGuardrail(guardrail_function=f,# If not set, guardrail name uses the function’s name by default.name=nameifnameelsef.__name__,run_in_parallel=run_in_parallel,)iffuncisnotNone:# Decorator was used without parenthesesreturndecorator(func)# Decorator used with keyword argumentsreturndecorator

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(...): ...
Source code insrc/agents/guardrail.py
defoutput_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 an `OutputGuardrail`.    It can be used directly (no parentheses) or with keyword args, e.g.:        @output_guardrail        def my_sync_guardrail(...): ...        @output_guardrail(name="guardrail_name")        async def my_async_guardrail(...): ...    """defdecorator(f:_OutputGuardrailFuncSync[TContext_co]|_OutputGuardrailFuncAsync[TContext_co],)->OutputGuardrail[TContext_co]:returnOutputGuardrail(guardrail_function=f,# Guardrail name defaults to function's name when not specified (None).name=nameifnameelsef.__name__,)iffuncisnotNone:# Decorator was used without parenthesesreturndecorator(func)# Decorator used with keyword argumentsreturndecorator

[8]ページ先頭

©2009-2025 Movatter.jp