feat: Expose tool call arguments in ToolContext and lifecycle hooksFeat/tool context arguments #1598
+94
−11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR addresses Issue #939 by enhancing the observability of tool calls within the Agents SDK. It exposes the raw JSON arguments of a tool call in the
ToolContext
, making them accessible in lifecycle hooks likeon_tool_start
andon_tool_end
.Why These Changes Are Required and Their Importance
Currently, developers using lifecycle hooks have visibility into which tool is being called (
tool_name
) but lack access to the arguments being passed to it. This limits the ability to effectively log, debug, or implement custom logic based on the tool's inputs.This change introduces an
arguments
field to theToolContext
, providing the raw JSON string of the arguments sent by the model. The importance of this enhancement is threefold:This aligns with the SDK's core goals of extensibility and traceability, giving developers deeper insights into the agent's execution flow.
Examples
The following examples demonstrate how to access
context.arguments
within a customRunHooks
implementation across synchronous, asynchronous, and streaming executions.1. Hello World (Sync)
This example shows a simple agent with a custom hook that prints the arguments passed to the
say_hello
tool during a synchronous run.2. Hello World (Async)
This example demonstrates the same argument logging hook within an asynchronous agent run.
4. Math World (Sync)
This example demonstrates argument logging for an agent that performs multiple tool calls in a synchronous run.
5. Math World (Async)
This shows the same multi-tool agent using an asynchronous runner.
6. Math World (Streaming)
Finally, this example confirms the hooks work as expected during a streaming run with a multi-tool agent.