Skip to content

Python: Preserve OTel parent context for deferred streams - #6709

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 2 commits into
microsoft:mainfrom
eavanvalkenburg:python-hosting-otel-stream-parent-context
Jun 25, 2026
Merged

Python: Preserve OTel parent context for deferred streams#6709
Eduard van Valkenburg (eavanvalkenburg) merged 2 commits into
microsoft:mainfrom
eavanvalkenburg:python-hosting-otel-stream-parent-context

Conversation

@eavanvalkenburg

Copy link
Copy Markdown
Member

Motivation & Context

Streaming hosting paths can defer target execution until after the route handler has returned a streaming response. In that deferred phase, inbound request span context may no longer be current, so agent/chat/tool spans can lose strict parent-child linkage. This change preserves parent context for deferred streaming in shared hosting stream adapters and adds regression coverage for both host-level and Responses-channel paths.

Description & Review Guide

  • What are the major changes?
    • Capture current OpenTelemetry context when host-managed streaming is opened.
    • Re-activate the captured context during deferred stream pulls and get_final_response() in _BoundResponseStream.
    • Add host-level regression test proving deferred consumption still observes the expected parent span context.
    • Add Responses-channel integration test proving streaming request handling keeps parent span propagation.
  • What is the impact of these changes?
    • Improves trace correctness for hosted streaming scenarios by restoring strict parent-child span linkage when an inbound request span exists.
    • Applies host-wide, so all channels using shared host streaming behavior benefit, while preserving existing stream lifecycle semantics.
  • What do you want reviewers to focus on?
    • Context capture/activation boundaries in _BoundResponseStream and whether the new behavior preserves existing close/finalization guarantees.

Related Issue

Fixes #6708

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

- capture current OTel context when opening host-managed streaming runs
- re-activate captured context during deferred stream pulls and finalization
- add host-level regression coverage for deferred stream parent-span linkage
- add Responses channel integration coverage for request parent span propagation

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 24, 2026 13:19
@moonbox3 Evan Mattson (moonbox3) added the python Usage: [Issues, PRs], Target: Python label Jun 24, 2026
@github-actions

github-actions Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/hosting/agent_framework_hosting
   _host.py4625089%90, 145, 160, 188, 258, 268–270, 272–273, 279, 402, 410–411, 414, 416, 423, 429, 432–434, 437, 455, 781–783, 790–794, 800–801, 804, 810–811, 958–959, 966, 1074, 1111, 1282–1285, 1367–1368, 1373–1375
TOTAL42174498188% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
8305 37 💤 0 ❌ 0 🔥 2m 8s ⏱️

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

Automated Code Review

Reviewers: 5 | Confidence: 91% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Failure Modes, Design Approach


Automated review by eavanvalkenburg's agents

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

This PR improves OpenTelemetry trace parent/child linkage for deferred streaming in the Python hosting layer by capturing the active OTel context when a stream is opened and re-activating it during later stream pulls and finalization, with regression tests covering both host-level behavior and the Responses-channel SSE path.

Changes:

  • Capture the current OTel context at host-managed stream construction time and re-activate it during deferred iteration and get_final_response() in _BoundResponseStream.
  • Add a host-level regression test that simulates deferred consumption (construct stream under a parent span, consume after the span context is detached).
  • Add a Responses-channel test asserting streaming invocations observe the request’s parent span context.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
python/packages/hosting/agent_framework_hosting/_host.py Captures and re-activates OTel context for deferred streaming via _BoundResponseStream.
python/packages/hosting/tests/hosting/test_host.py Adds a regression test ensuring deferred stream pulls/finalization preserve the captured parent span context.
python/packages/hosting-responses/tests/hosting_responses/test_channel.py Adds a Responses-channel SSE test to validate parent span context propagation during streaming requests.

Comment thread python/packages/hosting/agent_framework_hosting/_host.py
- capture OTel context snapshot before invoking target.run in _invoke_stream
- add regression test guarding capture-before-run evaluation order

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@TaoChenOSU

Copy link
Copy Markdown
Contributor

nit: Can you add two screenshots for before and after the fix showing the parent child relationship? It can be any monitoring backend.

Merged via the queue into microsoft:main with commit ce74c84 Jun 25, 2026
37 checks passed
@eavanvalkenburg

Copy link
Copy Markdown
Member Author

Tao Chen (@TaoChenOSU) This is after, don't have a running from before anymore:
image

@eavanvalkenburg
Eduard van Valkenburg (eavanvalkenburg) deleted the python-hosting-otel-stream-parent-context branch June 30, 2026 09:02
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: Python hosting: preserve OTel parent span for deferred streaming

5 participants