-
Notifications
You must be signed in to change notification settings - Fork 847
Fix OpenAIEmbeddingGenerator to handle missing usage data #7074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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.Usageto 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 |
Fixes #7070
Microsoft Reviewers: Open in CodeFlow