Python: Preserve OTel parent context for deferred streams#6709
Open
eavanvalkenburg wants to merge 2 commits into
Open
Python: Preserve OTel parent context for deferred streams#6709eavanvalkenburg wants to merge 2 commits into
eavanvalkenburg wants to merge 2 commits into
Conversation
- 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>
Contributor
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
Contributor
There was a problem hiding this comment.
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. |
- 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>
Contributor
|
nit: Can you add two screenshots for before and after the fix showing the parent child relationship? It can be any monitoring backend. |
TaoChenOSU
approved these changes
Jun 24, 2026
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 & 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
get_final_response()in_BoundResponseStream._BoundResponseStreamand whether the new behavior preserves existing close/finalization guarantees.Related Issue
Fixes #6708
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.