feat: expose tool_call_id on RunContextWrapper for lifecycle hooks#2915
Open
nileshpatil6 wants to merge 1 commit intoopenai:mainfrom
Open
feat: expose tool_call_id on RunContextWrapper for lifecycle hooks#2915nileshpatil6 wants to merge 1 commit intoopenai:mainfrom
nileshpatil6 wants to merge 1 commit intoopenai:mainfrom
Conversation
Add a `tool_call_id: str | None` field (init=False, default None) to `RunContextWrapper` so that callers of `on_tool_start` / `on_tool_end` hooks can read the tool call ID directly from the context parameter without needing an `isinstance(context, ToolContext)` guard. `ToolContext` already declares `tool_call_id` as a required init field (str, not str | None), so subclass behaviour is unchanged. The base field is `init=False` to avoid clashing with `ToolContext.from_agent_context` which copies base-class init fields by name. Both `_fork_with_tool_input` and `_fork_without_tool_input` are updated to propagate the value when forking. Closes openai#1849
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes #1849.
on_tool_startandon_tool_endhooks receive aRunContextWrapperas their context parameter. When the invocation is a function-tool call, the runtime actually passes aToolContext(a subclass) which already carriestool_call_id. However, users who want to correlate parallel tool calls in observability/metrics code were forced to write:This PR adds
tool_call_id: str | Noneas aninit=Falsefield (defaultNone) directly onRunContextWrapper, so the pattern becomes:Changes
src/agents/run_context.py— addstool_call_id: str | None = field(default=None, init=False)toRunContextWrapper; propagates the value through_fork_with_tool_inputand_fork_without_tool_input.tests/test_run_hooks.py— adds two tests:test_tool_call_id_exposed_on_run_context_wrapper— verifies the correctcall_idis surfaced in bothon_tool_startandon_tool_endwithout anyisinstancecheck.test_tool_call_id_is_none_outside_tool_context— verifies the field isNoneon a plainRunContextWrapper.Compatibility
ToolContextredeclarestool_call_idas a requiredinit=Truefield (str, notstr | None), so subclass behaviour and construction are unchanged.init=Falseto avoid clashing withToolContext.from_agent_context, which copies base-class init fields by name.tests/sandbox/,tests/test_run_state.py,tests/test_sandbox_memory.py) fail due to the missingfcntlmodule on Windows — this is pre-existing and unrelated to this change.Test plan
uv run pytest tests/test_run_hooks.py tests/test_function_tool.py tests/test_tool_context.py— 68 passeduv run pyright src/agents/run_context.py src/agents/tool_context.py— 0 errors