diff --git a/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/AIContent.cs b/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/AIContent.cs index 25a21af5f16..06798f10f3d 100644 --- a/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/AIContent.cs +++ b/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/AIContent.cs @@ -5,7 +5,7 @@ namespace Microsoft.Extensions.AI; -/// Provides a base class for all content used with AI services. +/// Represents content used by AI services. [JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")] [JsonDerivedType(typeof(DataContent), typeDiscriminator: "data")] [JsonDerivedType(typeof(ErrorContent), typeDiscriminator: "error")] @@ -20,7 +20,7 @@ public class AIContent /// /// Initializes a new instance of the class. /// - protected AIContent() + public AIContent() { } @@ -28,7 +28,7 @@ protected AIContent() /// /// If an is created to represent some underlying object from another object /// model, this property can be used to store that original object. This can be useful for debugging or - /// for enabling a consumer to access the underlying object model if needed. + /// for enabling a consumer to access the underlying object model, if needed. /// [JsonIgnore] public object? RawRepresentation { get; set; } diff --git a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/AIContentTests.cs b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/AIContentTests.cs index 027fd61649c..64a20fc5e4a 100644 --- a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/AIContentTests.cs +++ b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/AIContentTests.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Text.Json; using Xunit; namespace Microsoft.Extensions.AI; @@ -10,7 +11,7 @@ public class AIContentTests [Fact] public void Constructor_PropsDefault() { - DerivedAIContent c = new(); + AIContent c = new(); Assert.Null(c.RawRepresentation); Assert.Null(c.AdditionalProperties); } @@ -18,7 +19,7 @@ public void Constructor_PropsDefault() [Fact] public void Constructor_PropsRoundtrip() { - DerivedAIContent c = new(); + AIContent c = new(); Assert.Null(c.RawRepresentation); object raw = new(); @@ -31,5 +32,25 @@ public void Constructor_PropsRoundtrip() Assert.Same(props, c.AdditionalProperties); } - private sealed class DerivedAIContent : AIContent; + [Fact] + public void Serialization_Roundtrips() + { + AIContent original = new() + { + RawRepresentation = new object(), + AdditionalProperties = new AdditionalPropertiesDictionary { { "key", "value" } } + }; + + Assert.NotNull(original.RawRepresentation); + Assert.Single(original.AdditionalProperties); + + string json = JsonSerializer.Serialize(original, AIJsonUtilities.DefaultOptions.GetTypeInfo(typeof(AIContent))); + Assert.NotNull(json); + + AIContent? deserialized = (AIContent?)JsonSerializer.Deserialize(json, AIJsonUtilities.DefaultOptions.GetTypeInfo(typeof(AIContent))); + Assert.NotNull(deserialized); + Assert.Null(deserialized.RawRepresentation); + Assert.NotNull(deserialized.AdditionalProperties); + Assert.Single(deserialized.AdditionalProperties); + } }