Python: preserve tool span context for parallel calls - #6512
Python: preserve tool span context for parallel calls#6512Evan Mattson (moonbox3) merged 6 commits into
Conversation
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. |
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
|
ByteWise (@2830500285) there have been some updates in the underlying codebase, so some checks are failing because of that, could you check them? |
|
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. |
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)