-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix MIME type handling for image URIs in DataContent and UriContent constructors #3393
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/Responses/Converters/ItemContentConverter.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
@copilot, can you add tests for this? |
dotnet/src/Microsoft.Agents.AI.Workflows.Declarative/Extensions/ChatMessageExtensions.cs
Outdated
Show resolved
Hide resolved
|
@copilot You are working so hard - good bot. |
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Added comprehensive tests! Created test files covering:
All tests pass successfully. (commit c1c2930) |
…t MIME type inference 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 fixes MIME type handling for image URIs to support providers like Gemini that require specific MIME types (e.g., image/png, image/jpeg) rather than the generic "image/*".
Changes:
- Removed hardcoded
"image/*"override for data URIs, allowingDataContentconstructor to parse MIME types from data URI format - Added
ImageUriToMediaTypehelper method to infer MIME types from file extensions for regular HTTP/HTTPS URIs - Added comprehensive test coverage (46 new tests) for data URI MIME type preservation, HTTP URI extension inference, and edge cases
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
ItemContentConverter.cs |
Added ImageUriToMediaType helper and updated image handling to preserve data URI MIME types and infer from extensions |
MessageContentPartConverter.cs |
Removed "image/*" override for data URIs, added ImageUriToMediaType helper for extension-based inference |
ChatMessageExtensions.cs |
Added ImageUriToMediaType helper and updated GetImageContent to properly handle data URIs and infer MIME types |
ItemContentConverterTests.cs |
Added 18 tests covering data URI MIME type preservation, HTTP URI extension inference, and edge cases |
MessageContentPartConverterTests.cs |
Added 24 tests covering image data/HTTP URIs, audio formats, file content, and edge cases |
ChatMessageExtensionsTests.cs |
Added 4 tests for data URI vs HTTP URI MIME type behavior with round-trip conversion testing |
|
@copilot there are formatting diagnostics: |
…in tests Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
... Fixed! Updated all const local variable names to use PascalCase (DataUri, Uri, Text, AudioData, FileId, Filename) to comply with IDE1006 naming rules. All tests continue to pass. (commit 3edd739) |
Motivation and Context
Providers like Gemini reject the generic
"image/*"MIME type and require specific types (e.g.,image/png,image/jpeg). When data URIs containing explicit MIME types are passed, the hardcoded"image/*"parameter overrides the actual type, causing "Bad Request" errors. Additionally, regular image URIs were not inferring MIME types from file extensions.Description
Changes for data URIs:
Removed the
"image/*"parameter fromDataContentconstructor calls when processing data URIs. The constructor already extracts MIME types from data URI format (data:image/png;base64,...).Changes for regular URIs:
Added
ImageUriToMediaTypehelper method to all three converter files to infer MIME types from file extensions (e.g.,.png→image/png,.jpg→image/jpeg,.gif→image/gif, etc.). UpdatedUriContentconstructions to use this helper instead of hardcoded"image/*".Files modified:
ItemContentConverter.cs- Responses API converter (added helper method and updated both DataContent and UriContent constructions)MessageContentPartConverter.cs- Chat completions converter (updated DataContent construction)ChatMessageExtensions.cs- Declarative workflows helper (added helper method and updated both DataContent and UriContent constructions)Before:
After:
The
DataContentconstructor parses the MIME type from the data URI when no override is provided, andImageUriToMediaTypeinfers the type from file extensions for regular URIs, falling back to"image/*"for unknown extensions. This ensures consistent MIME type handling across all three converters.Test coverage added:
All const local variables in tests follow PascalCase naming convention per IDE1006 requirements.
Contribution Checklist
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.