Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 5, 2026

Adds temporary support for the InputImageUrl property from OpenAI API responses using reflection, since the OpenAI .NET library doesn't yet expose this property publicly (pending openai/openai-dotnet#874).

Changes Made

  • Reflection-based property access: Added cached PropertyInfo using Type.GetType("OpenAI.Responses.InternalItemContentInputImage, OpenAI")?.GetProperty("ImageUrl") to access the internal ImageUrl property without triggering trimming warnings
  • Content type handling: Modified ToAIContents method to properly handle image URLs:
    • HTTP/HTTPS URLs are converted to UriContent
    • Data URIs (starting with data:) are converted to DataContent
    • Uses Uri.TryCreate for defensive URI parsing to handle potentially malformed URLs gracefully
  • Unit tests: Added comprehensive tests validating both HTTP URL (returns UriContent) and data URI (returns DataContent) scenarios

The reflection code includes a comment citing the upstream PR so it can be replaced with the actual public property once available.

Original prompt

The OpenAIResponsesChatClient's ToAIContent method is missing support for part.InputImageUrl because the underlying library doesn't expose that property yet (it's being added in openai/openai-dotnet#874). Until it's exposed, though, please handle it via reflection, e.g. part.GetType().GetProperty("ImageUrl")?.GetValue(part, null) as string, with a comment citing openai/openai-dotnet#874 and indicating that it should be replaced by the real public property once it's available. Please augment or add a unit test that validates the data propagates out to the request body.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Microsoft Reviewers: Open in CodeFlow

…ient

This adds handling for the `image_url` property in `input_image` response content.
Since the OpenAI SDK doesn't yet expose this property publicly, we access it via:
1. Direct property reflection (for when the property becomes public)
2. SerializedAdditionalRawData dictionary (for internal SDK storage)
3. IJsonModel serialization fallback (to extract from JSON representation)

See: openai/openai-dotnet#874

Includes a unit test that validates the image URL propagates through responses.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Address code review feedback by using Uri.TryCreate instead of
new Uri() to gracefully handle invalid URI formats.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Add reflection support for part.InputImageUrl property Add support for InputImageUrl via reflection in OpenAIResponsesChatClient Jan 5, 2026
Copilot AI requested a review from stephentoub January 5, 2026 17:06
…ImageUrl")

Address code review feedback by using the simpler inline reflection pattern
as originally suggested, instead of static PropertyInfo fields and fallbacks.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…GetType()

Cache PropertyInfo using Type.GetType("OpenAI.Responses.InternalItemContentInputImage, OpenAI")
to avoid the IL2075 trimming warning and remove the #pragma disable.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@stephentoub stephentoub marked this pull request as ready for review January 5, 2026 17:50
@stephentoub stephentoub requested a review from a team as a code owner January 5, 2026 17:50
Copilot AI review requested due to automatic review settings January 5, 2026 17:50
Check if the image URL starts with "data:" and parse as DataContent in
that case. HTTP/HTTPS URLs continue to be parsed as UriContent. Added
separate tests for both cases.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
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 PR adds temporary support for the InputImageUrl property from OpenAI API responses using reflection, since the OpenAI .NET library doesn't yet expose this property publicly (pending openai/openai-dotnet#874).

Key Changes

  • Added reflection-based access to the internal ImageUrl property of InternalItemContentInputImage type
  • Modified ToAIContents method to handle image URLs from API responses and convert them to UriContent
  • Added comprehensive unit test to verify image URLs properly propagate through response parsing

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponsesChatClient.cs Implements reflection-based property access and integrates InputImageUrl handling into the content conversion logic
test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIResponseClientTests.cs Adds test case validating that input_image content with image_url is correctly parsed into UriContent

Refactored the ternary expression to an if-else block and replaced
new Uri() with Uri.TryCreate() for safer handling of potentially
malformed URLs.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
The pattern match `is string inputImageUrl` already handles null values,
and empty/whitespace strings will be handled by the subsequent checks.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@stephentoub stephentoub merged commit 035db1f into main Jan 5, 2026
6 checks passed
@stephentoub stephentoub deleted the copilot/support-input-image-url branch January 5, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants