Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Microsoft.Extensions.AI;

/// <summary>Provides a base class for all content used with AI services.</summary>
/// <summary>Represents content used by AI services.</summary>
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$type")]
[JsonDerivedType(typeof(DataContent), typeDiscriminator: "data")]
[JsonDerivedType(typeof(ErrorContent), typeDiscriminator: "error")]
Expand All @@ -20,15 +20,15 @@ public class AIContent
/// <summary>
/// Initializes a new instance of the <see cref="AIContent"/> class.
/// </summary>
protected AIContent()
public AIContent()
{
}

/// <summary>Gets or sets the raw representation of the content from an underlying implementation.</summary>
/// <remarks>
/// If an <see cref="AIContent"/> 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.
/// </remarks>
[JsonIgnore]
public object? RawRepresentation { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,15 +11,15 @@ public class AIContentTests
[Fact]
public void Constructor_PropsDefault()
{
DerivedAIContent c = new();
AIContent c = new();
Assert.Null(c.RawRepresentation);
Assert.Null(c.AdditionalProperties);
}

[Fact]
public void Constructor_PropsRoundtrip()
{
DerivedAIContent c = new();
AIContent c = new();

Assert.Null(c.RawRepresentation);
object raw = new();
Expand All @@ -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);
}
}
Loading