-
Notifications
You must be signed in to change notification settings - Fork 849
Add support for InputImageUrl via reflection in OpenAIResponsesChatClient #7195
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
…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>
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponsesChatClient.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponsesChatClient.cs
Outdated
Show resolved
Hide resolved
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>
…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>
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponsesChatClient.cs
Outdated
Show resolved
Hide resolved
…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>
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponsesChatClient.cs
Outdated
Show resolved
Hide resolved
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>
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 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
ImageUrlproperty ofInternalItemContentInputImagetype - Modified
ToAIContentsmethod to handle image URLs from API responses and convert them toUriContent - 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 |
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponsesChatClient.cs
Outdated
Show resolved
Hide resolved
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>
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponsesChatClient.cs
Outdated
Show resolved
Hide resolved
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>
Adds temporary support for the
InputImageUrlproperty 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
PropertyInfousingType.GetType("OpenAI.Responses.InternalItemContentInputImage, OpenAI")?.GetProperty("ImageUrl")to access the internalImageUrlproperty without triggering trimming warningsToAIContentsmethod to properly handle image URLs:UriContentdata:) are converted toDataContentUri.TryCreatefor defensive URI parsing to handle potentially malformed URLs gracefullyUriContent) and data URI (returnsDataContent) scenariosThe reflection code includes a comment citing the upstream PR so it can be replaced with the actual public property once available.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.
Microsoft Reviewers: Open in CodeFlow