Skip to content

Map JSON DataContent to Part.Data instead of Part.Raw #447

Description

@SergeyMenshykh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions