Python: preserve tool span context for parallel calls#6512
Open
2830500285 wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds a regression test and updates parallel tool invocation to preserve active tracing context across concurrent executions.
Changes:
- Add a new observability test validating parallel tool execution spans are nested under the agent invoke span.
- Update parallel tool execution to create asyncio tasks under a copied
contextvarscontext to preserve tracing context propagation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| python/packages/core/tests/core/test_observability.py | Adds a regression test covering parent/trace relationships for parallel tool spans. |
| python/packages/core/agent_framework/_tools.py | Wraps task creation with contextvars.copy_context() to keep active span context in parallel tool execution. |
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.
Motivation and Context
Fixes #6357.
Parallel function calls are scheduled together during tool invocation. Each scheduled tool execution should inherit the active agent invocation context so its
execute_toolspan stays correctly parented and trace-linked when multiple tool calls run in the same turn.Description
This updates the parallel tool invocation path to create each task from an explicit copy of the current
contextvarscontext before gathering the results.It also adds an observability regression test that drives two parallel tool calls through an agent and asserts both
execute_toolspans are present, share the agent trace, and use the agent span as parent.Contribution Checklist
agent_framework_toolsshell imports and one hosted-environment import-mock test)