Skip to content

fix(hooks): handle kwargs in hook calls #1002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/agents/_run_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,19 +553,19 @@ async def run_single_tool(
span_fn.span_data.input = tool_call.arguments
try:
_, _, result = await asyncio.gather(
hooks.on_tool_start(tool_context, agent, func_tool),
hooks.on_tool_start(context=tool_context, agent=agent, tool=func_tool),
(
agent.hooks.on_tool_start(tool_context, agent, func_tool)
agent.hooks.on_tool_start(context=tool_context, agent=agent, tool=func_tool)
if agent.hooks
else _coro.noop_coroutine()
),
func_tool.on_invoke_tool(tool_context, tool_call.arguments),
)

await asyncio.gather(
hooks.on_tool_end(tool_context, agent, func_tool, result),
hooks.on_tool_end(context=tool_context, agent=agent, tool=func_tool, result=result),
(
agent.hooks.on_tool_end(tool_context, agent, func_tool, result)
agent.hooks.on_tool_end(context=tool_context, agent=agent, tool=func_tool, result=result)
if agent.hooks
else _coro.noop_coroutine()
),
Expand Down Expand Up @@ -874,8 +874,8 @@ async def run_final_output_hooks(
final_output: Any,
):
await asyncio.gather(
hooks.on_agent_end(context_wrapper, agent, final_output),
agent.hooks.on_end(context_wrapper, agent, final_output)
hooks.on_agent_end(context=context_wrapper, agent=agent, output=final_output),
agent.hooks.on_end(context=context_wrapper, agent=agent, output=final_output)
if agent.hooks
else _coro.noop_coroutine(),
)
Expand Down Expand Up @@ -1035,19 +1035,19 @@ async def execute(
)

_, _, output = await asyncio.gather(
hooks.on_tool_start(context_wrapper, agent, action.computer_tool),
hooks.on_tool_start(context=context_wrapper, agent=agent, tool=action.computer_tool),
(
agent.hooks.on_tool_start(context_wrapper, agent, action.computer_tool)
agent.hooks.on_tool_start(context=context_wrapper, agent=agent, tool=action.computer_tool)
if agent.hooks
else _coro.noop_coroutine()
),
output_func,
)

await asyncio.gather(
hooks.on_tool_end(context_wrapper, agent, action.computer_tool, output),
hooks.on_tool_end(context=context_wrapper, agent=agent, tool=action.computer_tool, result=output),
(
agent.hooks.on_tool_end(context_wrapper, agent, action.computer_tool, output)
agent.hooks.on_tool_end(context=context_wrapper, agent=agent, tool=action.computer_tool, result=output)
if agent.hooks
else _coro.noop_coroutine()
),
Expand Down Expand Up @@ -1138,9 +1138,9 @@ async def execute(
config: RunConfig,
) -> RunItem:
await asyncio.gather(
hooks.on_tool_start(context_wrapper, agent, call.local_shell_tool),
hooks.on_tool_start(context=context_wrapper, agent=agent, tool=call.local_shell_tool),
(
agent.hooks.on_tool_start(context_wrapper, agent, call.local_shell_tool)
agent.hooks.on_tool_start(context=context_wrapper, agent=agent, tool=call.local_shell_tool)
if agent.hooks
else _coro.noop_coroutine()
),
Expand All @@ -1157,9 +1157,9 @@ async def execute(
result = output

await asyncio.gather(
hooks.on_tool_end(context_wrapper, agent, call.local_shell_tool, result),
hooks.on_tool_end(context=context_wrapper, agent=agent, tool=call.local_shell_tool, result=result),
(
agent.hooks.on_tool_end(context_wrapper, agent, call.local_shell_tool, result)
agent.hooks.on_tool_end(context=context_wrapper, agent=agent, tool=call.local_shell_tool, result=result)
if agent.hooks
else _coro.noop_coroutine()
),
Expand Down
4 changes: 3 additions & 1 deletion src/agents/guardrail.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ async def my_async_guardrail(...): ...
def decorator(
f: _InputGuardrailFuncSync[TContext_co] | _InputGuardrailFuncAsync[TContext_co],
) -> InputGuardrail[TContext_co]:
return InputGuardrail(guardrail_function=f, name=name)

# The function name should be the name of the guardrail if no name is defined
return InputGuardrail(guardrail_function=f, name=name if name else f._name_)

if func is not None:
# Decorator was used without parentheses
Expand Down
8 changes: 4 additions & 4 deletions src/agents/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,9 @@ async def _run_single_turn_streamed(
) -> SingleStepResult:
if should_run_agent_start_hooks:
await asyncio.gather(
hooks.on_agent_start(context_wrapper, agent),
hooks.on_agent_start(context=context_wrapper, agent=agent),
(
agent.hooks.on_start(context_wrapper, agent)
agent.hooks.on_start(context=context_wrapper, agent=agent)
if agent.hooks
else _coro.noop_coroutine()
),
Expand Down Expand Up @@ -889,9 +889,9 @@ async def _run_single_turn(
# Ensure we run the hooks before anything else
if should_run_agent_start_hooks:
await asyncio.gather(
hooks.on_agent_start(context_wrapper, agent),
hooks.on_agent_start(context=context_wrapper, agent=agent),
(
agent.hooks.on_start(context_wrapper, agent)
agent.hooks.on_start(context=context_wrapper, agent=agent)
if agent.hooks
else _coro.noop_coroutine()
),
Expand Down
Loading