-
Notifications
You must be signed in to change notification settings - Fork 849
Add LoadFromAsync and SaveToAsync helper methods to DataContent #7159
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
stephentoub
left a comment
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.
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/MediaTypeMap.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/MediaTypeMap.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/MediaTypeMap.cs
Outdated
Show resolved
Hide resolved
|
All review comments addressed in 840f61c. Changes include: MediaTypeMap moved to LegacySupport with full mappings, methods are now stable (not experimental), DefaultMediaType moved to DataContent as private const, using GetBuffer() for MemoryStream, proper null handling for Name, SaveToAsync handles directory paths and always infers extensions, and NET9_0_OR_GREATER conditional for ReadOnlyMemory overload. |
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
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 helper methods to DataContent for loading from and saving to file system paths and streams, with automatic media type (MIME type) inference based on file extensions.
Key Changes
- Adds static
LoadFromAsyncmethods (path and stream overloads) that infer media types and names from file extensions - Adds instance
SaveToAsyncmethod that handles directory paths and extension inference - Implements
MediaTypeMappolyfill for .NET 9 and earlier to enable file extension-to-MIME type mapping - Marks the new APIs as stable (non-experimental) in the JSON baseline
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs |
Implements the new LoadFromAsync and SaveToAsync methods with media type inference logic |
src/LegacySupport/MediaTypeMap/MediaTypeMap.cs |
Adds polyfill implementation of media type mapping for pre-.NET 10 frameworks |
src/LegacySupport/MediaTypeMap/README.md |
Documents the MediaTypeMap polyfill purpose and origin |
eng/MSBuild/LegacySupport.props |
Configures MSBuild to inject MediaTypeMap polyfill for legacy target frameworks |
src/Libraries/Microsoft.Extensions.AI.Abstractions/Microsoft.Extensions.AI.Abstractions.csproj |
Enables MediaTypeMap injection for the abstractions project |
src/Libraries/Microsoft.Extensions.AI.Abstractions/Microsoft.Extensions.AI.Abstractions.json |
Adds the new methods to the stable API baseline |
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/DataContentTests.cs |
Adds comprehensive unit tests covering all new functionality and edge cases |
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIImageGenerator.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs
Show resolved
Hide resolved
…peMap Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
…ontent methods Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
…Array in SaveToAsync Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
…add empty path test Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
…mAsync Co-authored-by: stephentoub <[email protected]>
Co-authored-by: stephentoub <[email protected]>
ced6113 to
bd2f87a
Compare
|
@copilot, there are build failures, e.g. |
Co-authored-by: stephentoub <[email protected]>
Head branch was pushed to by a user without write access
System.Net.MimenamespaceLoadFromAsyncmethods toDataContentclass:SaveToAsyncmethod toDataContentclass:Path.GetFileName(Name)[Experimental]attribute and add methods to JSON API baseline as stableGetBuffer()instead ofToArray()for MemoryStream efficiencyWriteAllBytesAsync(ReadOnlyMemory<byte>)polyfill to remove#ifdeffromSaveToAsyncTask<T>toValueTask<T>for all new public async methodsFile.ReadAllBytesAsyncandFile.WriteAllBytesAsyncin LegacySupport directorynameparameter fromLoadFromAsync(Stream)- Name can be assigned after the callInjectFilePolyfillsOnLegacyto test projectMarkItDownMcpReader.csto useDataContent.LoadFromAsyncwith optional media type parameterMicrosoft.Extensions.AI.Abstractionsproject reference to MarkItDown project forLoadFromAsyncaccessImageGeneratorExtensions.cs- Replace_extensionToMimeTypedictionary withMediaTypeMap.GetMediaTypeOpenAIClientExtensions.cs- ReplaceImageUriToMediaTypewithMediaTypeMap.GetMediaTypeOpenAIImageGenerator.cs- Replace_mimeTypeToExtensiondictionary withMediaTypeMap.GetExtensionIngestionDocumentReader.cs- Remove custom lookup, only useMediaTypeMap.GetMediaTypeOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Microsoft Reviewers: Open in CodeFlow