Skip to content

Guard against a null choice or delta in TryGetReasoningContent - #7792

Closed
eersinyildizz wants to merge 1 commit into
dotnet:mainfrom
eersinyildizz:fix-null-delta-reasoning-content
Closed

eersinyildizz wants to merge 1 commit into
dotnet:mainfrom
eersinyildizz:fix-null-delta-reasoning-content

Conversation

@eersinyildizz

@eersinyildizz eersinyildizz commented Sep 28, 2026 •

Copy link
Copy Markdown

Fixes #7790.

TryGetReasoningContent calls JsonElement.TryGetProperty on choices[0] and on
choices[0].delta without checking that either is an object. TryGetProperty requires
ValueKind == JsonValueKind.Object and throws otherwise, so a streaming chunk carrying
"delta": null (or "choices": [null]) fails the whole stream with:

System.InvalidOperationException: The requested operation requires an element of type 'Object', but the target element has type 'Null'.
   at System.Text.Json.ThrowHelper.ThrowJsonElementWrongTypeException(JsonTokenType expectedType, JsonTokenType actualType)
   at Microsoft.Extensions.AI.OpenAIChatClient.FromOpenAIStreamingChatCompletionAsync(...)

Azure OpenAI emits exactly this shape when content filtering runs in asynchronous mode: an
annotation-only event carrying content_filter_results / content_filter_offsets with a null
delta, interleaved with token chunks and also after finish_reason: "stop". The same backend
behaviour has had to be guarded against in openai-python (#2313), LiteLLM (#3811) and
Semantic Kernel (#8407).

The method is already written to fail soft - every other step is a ValueKind check or a
TryGetProperty - so the fix is to add the two missing kind checks. A null or otherwise
non-object element now means "no reasoning content here" instead of an exception.

Microsoft Reviewers: Open in CodeFlow

Copilot AI lite review requested due to automatic review settings September 28, 2026 10:44
@eersinyildizz
eersinyildizz requested a review from a team as a code owner September 28, 2026 10:44
@github-actions github-actions Bot added the area-ai Microsoft.Extensions.AI libraries label Sep 28, 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@dotnet-comment-bot

Copy link
Copy Markdown
Collaborator

‼️ Found issues ‼️

Project Coverage Type Expected Actual
Microsoft.Extensions.Diagnostics.Testing Line 99 98.65 🔻
Microsoft.Extensions.Telemetry Line 93 92.64 🔻
Microsoft.Extensions.AI.OpenAI Line 75 68.34 🔻
Microsoft.Extensions.AI.OpenAI Branch 75 57.69 🔻
Microsoft.Extensions.DataIngestion.MarkItDown Line 75 4.46 🔻
Microsoft.Extensions.DataIngestion.MarkItDown Branch 75 0 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring Line 99 96.03 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring Branch 99 92.76 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring.Kubernetes Line 99 97.73 🔻
Microsoft.Extensions.ServiceDiscovery.Dns Line 75 69.93 🔻
Microsoft.Extensions.ServiceDiscovery Line 75 68.11 🔻
Microsoft.Extensions.ServiceDiscovery Branch 75 71.43 🔻
Microsoft.Extensions.ServiceDiscovery.Abstractions Line 75 42.11 🔻
Microsoft.Extensions.ServiceDiscovery.Abstractions Branch 75 42.86 🔻
Microsoft.Extensions.ServiceDiscovery.Yarp Line 75 73.85 🔻
Microsoft.Extensions.ServiceDiscovery.Yarp Branch 75 70 🔻
Microsoft.Extensions.VectorData.Abstractions Line 75 37.39 🔻
Microsoft.Extensions.VectorData.Abstractions Branch 75 22.73 🔻

🎉 Good job! The coverage increased 🎉
Update MinCodeCoverage in the project files.

Project Expected Actual
Microsoft.Extensions.Http.Diagnostics 94 95
Microsoft.Gen.BuildMetadata 97 100
Microsoft.Gen.MetadataExtractor 57 73
Microsoft.Gen.MetricsReports 67 69
Microsoft.Extensions.AI.Abstractions 82 86
Microsoft.Extensions.AI.Evaluation.NLP 0 78
Microsoft.Extensions.Caching.Hybrid 82 85
Microsoft.Extensions.DataIngestion 75 89
Microsoft.Extensions.DataIngestion.Markdig 75 90
Microsoft.Extensions.Http.Resilience 97 100

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=1614493&view=codecoverage-tab

@jozkee

jozkee commented Oct 9, 2026

Copy link
Copy Markdown
Member

@eersinyildizz thanks, I think it would be better to get rid of the workaround now that the underlying library has fixed the issue. #7815.

@jozkee jozkee closed this Oct 9, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Microsoft.Extensions.AI.OpenAI] Streaming throws on Azure content-filter chunks with null delta (10.10.1)

4 participants