Skip to content

Python: preserve tool span context for parallel calls#6512

Open
2830500285 wants to merge 2 commits into
microsoft:mainfrom
2830500285:codex/fix-tool-otel-context
Open

Python: preserve tool span context for parallel calls#6512
2830500285 wants to merge 2 commits into
microsoft:mainfrom
2830500285:codex/fix-tool-otel-context

Conversation

@2830500285

Copy link
Copy Markdown

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_tool span 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 contextvars context before gathering the results.

It also adds an observability regression test that drives two parallel tool calls through an agent and asserts both execute_tool spans are present, share the agent trace, and use the agent span as parent.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible (targeted observability tests pass; the full core unit suite currently has unrelated local failures for missing agent_framework_tools shell imports and one hosted-environment import-mock test)
  • Is this a breaking change? No

Copilot AI review requested due to automatic review settings June 14, 2026 13:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 contextvars context 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.

Comment thread python/packages/core/tests/core/test_observability.py Outdated
Comment thread python/packages/core/agent_framework/_tools.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: execute_tool OTel spans intermittently lost due to missing context propagation in asyncio.gather

3 participants