Conversation
…ng trace context When ActivitySource has no listeners, CreateAndConfigureActivity returns null. The workaround for dotnet/runtime#47802 was unconditionally setting Activity.Current = activity (null), destroying trace context for all downstream middleware in the streaming pipeline. This applies the same fix from PR #7321 (FunctionInvokingChatClient) to: - OpenTelemetryChatClient - OpenTelemetrySpeechToTextClient - OpenTelemetryTextToSpeechClient - OpenTelemetryHostedFileClient Agent-Logs-Url: https://github.com/dotnet/extensions/sessions/53ad8d92-99ca-4075-a7ee-fd3a1551817e Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix OpenTelemetryChatClient streaming null activity issue
Guard Activity.Current restore with null check in OpenTelemetry streaming clients
Mar 31, 2026
stephentoub
approved these changes
Mar 31, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an OpenTelemetry streaming regression where Activity.Current could be unintentionally cleared (set to null) on each streamed chunk when no ActivitySource listeners are attached.
Changes:
- Guard
Activity.Current = activityrestoration withif (activity is not null)in streaming loops for multiple OpenTelemetry client wrappers. - Add a unit test asserting
Activity.Currentis preserved during streaming when the OpenTelemetryActivitySourcehas no listeners.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/OpenTelemetryChatClientTests.cs | Adds streaming test to ensure Activity.Current is preserved when no OTel listeners exist. |
| src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs | Prevents clearing Activity.Current during streaming when no activity was created. |
| src/Libraries/Microsoft.Extensions.AI/SpeechToText/OpenTelemetrySpeechToTextClient.cs | Same null-guard for streaming activity restoration in speech-to-text. |
| src/Libraries/Microsoft.Extensions.AI/TextToSpeech/OpenTelemetryTextToSpeechClient.cs | Same null-guard for streaming activity restoration in text-to-speech. |
| src/Libraries/Microsoft.Extensions.AI/Files/OpenTelemetryHostedFileClient.cs | Same null-guard for activity restoration in async enumeration of listed files. |
tarekgh
approved these changes
Mar 31, 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.
OpenTelemetryChatClient.GetStreamingResponseAsyncunconditionally setsActivity.Current = activityafter eachyield returnas a workaround for dotnet/runtime#47802. When_activitySource.HasListeners()isfalse,activityisnull, so this destroysActivity.Currentfor all downstream middleware on every streaming chunk. Same class of bug fixed forFunctionInvokingChatClientin #7321.Changes
if (activity is not null)in all four affected streaming methods:OpenTelemetryChatClientOpenTelemetrySpeechToTextClientOpenTelemetryTextToSpeechClientOpenTelemetryHostedFileClientStreaming_NoListeners_PreservesActivityCurrentverifyingActivity.Currentsurvives streaming when no listener is attached