Skip to content

Python: preserve tool span context for parallel calls - #6512

Merged
Evan Mattson (moonbox3) merged 6 commits into
microsoft:mainfrom
2830500285:codex/fix-tool-otel-context
Jul 13, 2026
Merged

Python: preserve tool span context for parallel calls#6512
Evan Mattson (moonbox3) merged 6 commits into
microsoft:mainfrom
2830500285:codex/fix-tool-otel-context

Conversation

@2830500285

Copy link
Copy Markdown
Contributor

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
@moonbox3 Evan Mattson (moonbox3) added the python Usage: [Issues, PRs], Target: Python label Jun 14, 2026

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
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _tools.py11628392%227–228, 405, 407, 420, 445–447, 455, 473, 487, 494, 501, 524, 526, 533, 541, 670, 704–706, 709–711, 713, 719, 770–772, 797, 823, 827, 865–867, 871, 893, 1044, 1056, 1063–1066, 1087, 1095, 1109–1111, 1461, 1553, 1581, 1603, 1611, 1669, 1716, 1723–1724, 1783, 1787, 1840, 1901–1902, 1913, 1985, 1999, 2002, 2015, 2018, 2041, 2048, 2058, 2062, 2139, 2192, 2214, 2270, 2349, 2546, 2612–2613, 2779–2780, 2871
TOTAL44095527488% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
8826 33 💤 0 ❌ 0 🔥 2m 20s ⏱️

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 8, 2026
@eavanvalkenburg

Copy link
Copy Markdown
Member

ByteWise (@2830500285) there have been some updates in the underlying codebase, so some checks are failing because of that, could you check them?

@2830500285

Copy link
Copy Markdown
Contributor Author

Pushed 805f419 to address the failing checks from the new test:\n\n- Match the mock chat client's _inner_get_response signature with BaseChatClient (Sequence/Mapping).\n- Assert span contexts before accessing span IDs/trace IDs.\n- Align the test-only type suppressions with the adjacent tool invocation telemetry test.\n\nLocal validation:\n- uv run pytest packages/core/tests/core/test_observability.py -k parallel_function_call_spans_nested_under_agent_span\n- uv run poe lint -P core\n- uv run poe fmt -P core\n- uv run poe test-typing -P core now has no diagnostics from this new test; my local run still reports the existing environment-only ty resolution issue for graphviz.backend.execute in tests/workflow/test_viz.py.\n\nThe new GitHub Actions runs for this commit are currently marked action_required with no jobs, so they likely need maintainer approval before CI can execute.

@moonbox3
Evan Mattson (moonbox3) added this pull request to the merge queue Jul 10, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 10, 2026
@moonbox3
Evan Mattson (moonbox3) added this pull request to the merge queue Jul 10, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 10, 2026
@moonbox3
Evan Mattson (moonbox3) added this pull request to the merge queue Jul 13, 2026
Merged via the queue into microsoft:main with commit 7f4cc29 Jul 13, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

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

4 participants