Skip to content

Conversation

@stephentoub
Copy link
Member

@stephentoub stephentoub commented Nov 21, 2025

Fixes #7070

Microsoft Reviewers: Open in CodeFlow

@stephentoub stephentoub requested a review from a team as a code owner November 21, 2025 14:29
Copilot AI review requested due to automatic review settings November 21, 2025 14:29
@stephentoub stephentoub enabled auto-merge (squash) November 21, 2025 14:29
@github-actions github-actions bot added the area-ai Microsoft.Extensions.AI libraries label Nov 21, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes a bug in OpenAIEmbeddingGenerator where missing or null usage data in the OpenAI API response would cause a NullReferenceException. The fix adds a null check before accessing usage properties and includes comprehensive test coverage for both scenarios.

  • Added null-safe handling of embeddings.Usage to prevent crashes when usage data is absent
  • Added two test cases covering missing and explicitly null usage fields in API responses

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIEmbeddingGenerator.cs Added null check for embeddings.Usage before accessing its properties, returning null UsageDetails when usage data is unavailable
test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIEmbeddingGeneratorTests.cs Added tests for missing usage field and explicitly null usage field scenarios

@stephentoub stephentoub merged commit 8c1ca00 into dotnet:main Nov 21, 2025
12 of 13 checks passed
stephentoub added a commit to jeffhandley/extensions that referenced this pull request Nov 21, 2025
This was referenced Dec 11, 2025
This was referenced Jan 5, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Jan 12, 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.

OpenAIEmbeddingGenerator.GenerateAsync throws a null reference exception when the response does not have a usage object

2 participants