From 9b07a1a933b1c1c198c3db8264b88a27b49e3493 Mon Sep 17 00:00:00 2001 From: Roman Khan Date: Fri, 4 Jul 2025 00:26:45 +0500 Subject: [PATCH 1/2] feat(output_guardrail): ensure function name is provided instead of defaulting to None --- src/agents/guardrail.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/agents/guardrail.py b/src/agents/guardrail.py index a96f0f7d7..61dd9157a 100644 --- a/src/agents/guardrail.py +++ b/src/agents/guardrail.py @@ -310,7 +310,8 @@ async def my_async_guardrail(...): ... def decorator( f: _OutputGuardrailFuncSync[TContext_co] | _OutputGuardrailFuncAsync[TContext_co], ) -> OutputGuardrail[TContext_co]: - return OutputGuardrail(guardrail_function=f, name=name) + #Updating default name None to the function name of guardrail_function if no name is provided. + return OutputGuardrail(guardrail_function=f, name=name if name else f.__name__) if func is not None: # Decorator was used without parentheses From 557faefaae926fe555858e10a2121b0e247ccb81 Mon Sep 17 00:00:00 2001 From: Roman Khan Date: Thu, 10 Jul 2025 13:29:39 +0500 Subject: [PATCH 2/2] fix: update comment to comply with linting rules --- src/agents/guardrail.py | 54 ++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/src/agents/guardrail.py b/src/agents/guardrail.py index 61dd9157a..d2ce79400 100644 --- a/src/agents/guardrail.py +++ b/src/agents/guardrail.py @@ -109,7 +109,9 @@ async def run( context: RunContextWrapper[TContext], ) -> InputGuardrailResult: if not callable(self.guardrail_function): - raise UserError(f"Guardrail function must be callable, got {self.guardrail_function}") + raise UserError( + f"Guardrail function must be callable, got {self.guardrail_function}" + ) output = self.guardrail_function(context, agent, input) if inspect.isawaitable(output): @@ -160,7 +162,9 @@ async def run( self, context: RunContextWrapper[TContext], agent: Agent[Any], agent_output: Any ) -> OutputGuardrailResult: if not callable(self.guardrail_function): - raise UserError(f"Guardrail function must be callable, got {self.guardrail_function}") + raise UserError( + f"Guardrail function must be callable, got {self.guardrail_function}" + ) output = self.guardrail_function(context, agent, agent_output) if inspect.isawaitable(output): @@ -183,11 +187,19 @@ async def run( # For InputGuardrail _InputGuardrailFuncSync = Callable[ - [RunContextWrapper[TContext_co], "Agent[Any]", Union[str, list[TResponseInputItem]]], + [ + RunContextWrapper[TContext_co], + "Agent[Any]", + Union[str, list[TResponseInputItem]], + ], GuardrailFunctionOutput, ] _InputGuardrailFuncAsync = Callable[ - [RunContextWrapper[TContext_co], "Agent[Any]", Union[str, list[TResponseInputItem]]], + [ + RunContextWrapper[TContext_co], + "Agent[Any]", + Union[str, list[TResponseInputItem]], + ], Awaitable[GuardrailFunctionOutput], ] @@ -215,9 +227,11 @@ def input_guardrail( def input_guardrail( - func: _InputGuardrailFuncSync[TContext_co] - | _InputGuardrailFuncAsync[TContext_co] - | None = None, + func: ( + _InputGuardrailFuncSync[TContext_co] + | _InputGuardrailFuncAsync[TContext_co] + | None + ) = None, *, name: str | None = None, ) -> ( @@ -284,15 +298,20 @@ def output_guardrail( def output_guardrail( - func: _OutputGuardrailFuncSync[TContext_co] - | _OutputGuardrailFuncAsync[TContext_co] - | None = None, + func: ( + _OutputGuardrailFuncSync[TContext_co] + | _OutputGuardrailFuncAsync[TContext_co] + | None + ) = None, *, name: str | None = None, ) -> ( OutputGuardrail[TContext_co] | Callable[ - [_OutputGuardrailFuncSync[TContext_co] | _OutputGuardrailFuncAsync[TContext_co]], + [ + _OutputGuardrailFuncSync[TContext_co] + | _OutputGuardrailFuncAsync[TContext_co] + ], OutputGuardrail[TContext_co], ] ): @@ -308,10 +327,17 @@ async def my_async_guardrail(...): ... """ def decorator( - f: _OutputGuardrailFuncSync[TContext_co] | _OutputGuardrailFuncAsync[TContext_co], + f: ( + _OutputGuardrailFuncSync[TContext_co] + | _OutputGuardrailFuncAsync[TContext_co] + ), ) -> OutputGuardrail[TContext_co]: - #Updating default name None to the function name of guardrail_function if no name is provided. - return OutputGuardrail(guardrail_function=f, name=name if name else f.__name__) + + return OutputGuardrail( + guardrail_function=f, + # Guardrail name defaults to function name when not specified (None). + name=name if name else f.__name__ + ) if func is not None: # Decorator was used without parentheses