Description
AIContentExtensions.ToPart() currently maps every Microsoft.Extensions.AI.DataContent instance to Part.Raw, regardless of its media type.
The reverse conversion already maps Part.Data to JSON DataContent. Making the forward conversion media-type-aware provides symmetric round-tripping and avoids representing structured JSON as opaque base64-encoded bytes.
Example
var json = JsonSerializer.SerializeToUtf8Bytes(new { key = "value" });
var content = new DataContent(json, "application/json");
Part? part = content.ToPart();
Current behavior
part.Raw contains the JSON bytes and part.Data is unset.
Expected behavior
For application/json and media types with a +json structured syntax suffix, such as application/problem+json:
- Parse the content as JSON.
- Set
Part.Data.
- Preserve the original media type.
- Surface a
JsonException when content declared as JSON is invalid.
Non-JSON DataContent should continue to map to Part.Raw.
Description
AIContentExtensions.ToPart()currently maps everyMicrosoft.Extensions.AI.DataContentinstance toPart.Raw, regardless of its media type.The reverse conversion already maps
Part.Datato JSONDataContent. Making the forward conversion media-type-aware provides symmetric round-tripping and avoids representing structured JSON as opaque base64-encoded bytes.Example
Current behavior
part.Rawcontains the JSON bytes andpart.Datais unset.Expected behavior
For
application/jsonand media types with a+jsonstructured syntax suffix, such asapplication/problem+json:Part.Data.JsonExceptionwhen content declared as JSON is invalid.Non-JSON
DataContentshould continue to map toPart.Raw.