Skip to content

OpenAI: Avoid null implicit conversions for image options - #7727

Merged
jeffhandley merged 1 commit into
mainfrom
agents/openai-argumentnullexception-repro
Sep 2, 2026
Merged

jeffhandley merged 1 commit into
mainfrom
agents/openai-argumentnullexception-repro

Conversation

@jozkee

@jozkee jozkee commented Sep 1, 2026 •

Copy link
Copy Markdown
Member

Summary

  • add regression coverage for preserving an unspecified image-generation output format
  • avoid throwing non-nullable OpenAI SDK string conversions for unsupported Chat Completions and Responses image-detail values
  • document why the nullable casts are required when the SDK exposes both nullable and non-nullable op_Implicit conversions

Context

The output-format ArgumentNullException reported 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 as ImageGenerationToolOutputFileFormat?, 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 untyped null fallback, which bound to the SDK's non-nullable string conversion and threw ArgumentNullException. 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-restore
  • OpenAIChatClientTests.DataContentMessage_Image_AdditionalProperty_InvalidDetail_NonStreaming
  • OpenAIResponseClientTests.UserMessageWithInvalidImageDetail_OmitsDetail
  • OpenAIConversionTests.AsOpenAIResponseTool_WithHostedImageGenerationToolWithoutMediaType_PreservesUnspecifiedOutputFileFormat

All targeted tests pass. The exact pre-10.7 output-format expression and both original image-detail fallbacks were also verified to fail with ArgumentNullException before their respective nullable casts.

Microsoft Reviewers: Open in CodeFlow

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

🟢 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.MediaType results in OutputFileFormat == null (so output_format is omitted).
  • Fixes image “detail” conversions for Chat Completions and Responses so unsupported AdditionalProperties["detail"] values produce a nullable null (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
jeffhandley merged commit 2a8df4c into main Sep 2, 2026
8 checks passed
@jozkee
jozkee deleted the agents/openai-argumentnullexception-repro branch September 2, 2026 16:06
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 3, 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.

3 participants