Repository navigation
OpenAI: Avoid null implicit conversions for image options - #7727
Merged
Merged
Conversation
Explicitly type nullable fallbacks for OpenAI SDK extensible structs so unsupported image-detail values do not invoke the throwing non-nullable string conversion. Add focused regression tests and document the existing output-format workaround introduced with the OpenAI 2.11 upgrade. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
Pull request overview
This PR hardens Microsoft.Extensions.AI.OpenAI’s image-option conversions against OpenAI .NET SDK implicit-conversion pitfalls that can throw ArgumentNullException when a null fallback binds to a non-nullable op_Implicit, and adds regression tests to ensure invalid/unspecified image options are omitted/preserved as intended.
Changes:
- Adds regression coverage ensuring an unspecified
HostedImageGenerationTool.Options.MediaTyperesults inOutputFileFormat == null(sooutput_formatis omitted). - Fixes image “detail” conversions for Chat Completions and Responses so unsupported
AdditionalProperties["detail"]values produce a nullablenull(avoiding non-nullable implicit string conversions that throw). - Adds request-shape tests verifying invalid image detail values are omitted from outbound JSON.
File summaries
| File | Description |
|---|---|
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponsesChatClient.cs |
Forces nullable null fallbacks for image output format and Responses image-detail conversions to avoid SDK non-nullable implicit conversion exceptions. |
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIChatClient.cs |
Forces nullable null fallback for Chat Completions image-detail conversion to avoid SDK implicit conversion exceptions. |
test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIConversionTests.cs |
Adds regression test asserting unspecified media type preserves OutputFileFormat == null. |
test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIChatClientTests.cs |
Adds test and updates helper to assert invalid detail values are omitted from the chat request payload. |
test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIResponseClientTests.cs |
Adds test asserting invalid detail values are omitted from the Responses request payload. |
Review details
- Files reviewed: 5/5 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
jeffhandley
approved these changes
Sep 2, 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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Summary
op_ImplicitconversionsContext
The output-format
ArgumentNullExceptionreported in #7721 was already fixed in #7544 while upgrading OpenAI from 2.10.0 to 2.11.0. That change explicitly typed the switch fallback asImageGenerationToolOutputFileFormat?, but it did not add focused unit coverage or explain why the cast was necessary.Auditing the MEAI source for the same pattern found two additional occurrences in the Chat Completions and Responses image-detail conversions. Unsupported
AdditionalProperties["detail"]value types selected an untypednullfallback, which bound to the SDK's non-nullable string conversion and threwArgumentNullException. This change makes those fallbacks explicitly nullable and verifies that unsupported detail values are omitted from requests.Validation
dotnet build test\Libraries\Microsoft.Extensions.AI.OpenAI.Tests\Microsoft.Extensions.AI.OpenAI.Tests.csproj -f net10.0 --no-restoreOpenAIChatClientTests.DataContentMessage_Image_AdditionalProperty_InvalidDetail_NonStreamingOpenAIResponseClientTests.UserMessageWithInvalidImageDetail_OmitsDetailOpenAIConversionTests.AsOpenAIResponseTool_WithHostedImageGenerationToolWithoutMediaType_PreservesUnspecifiedOutputFileFormatAll targeted tests pass. The exact pre-10.7 output-format expression and both original image-detail fallbacks were also verified to fail with
ArgumentNullExceptionbefore their respective nullable casts.Microsoft Reviewers: Open in CodeFlow