Skip to content

Commit a3654b0

Browse files
authored
Expose AIContent constructor (#6346)
1 parent 095e5f9 commit a3654b0

File tree

2 files changed

+27
-6
lines changed
  • src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents
  • test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents

2 files changed

+27
-6
lines changed

src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/AIContent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Microsoft.Extensions.AI;
77

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

2727
/// <summary>Gets or sets the raw representation of the content from an underlying implementation.</summary>
2828
/// <remarks>
2929
/// If an <see cref="AIContent"/> is created to represent some underlying object from another object
3030
/// model, this property can be used to store that original object. This can be useful for debugging or
31-
/// for enabling a consumer to access the underlying object model if needed.
31+
/// for enabling a consumer to access the underlying object model, if needed.
3232
/// </remarks>
3333
[JsonIgnore]
3434
public object? RawRepresentation { get; set; }

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/AIContentTests.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Text.Json;
45
using Xunit;
56

67
namespace Microsoft.Extensions.AI;
@@ -10,15 +11,15 @@ public class AIContentTests
1011
[Fact]
1112
public void Constructor_PropsDefault()
1213
{
13-
DerivedAIContent c = new();
14+
AIContent c = new();
1415
Assert.Null(c.RawRepresentation);
1516
Assert.Null(c.AdditionalProperties);
1617
}
1718

1819
[Fact]
1920
public void Constructor_PropsRoundtrip()
2021
{
21-
DerivedAIContent c = new();
22+
AIContent c = new();
2223

2324
Assert.Null(c.RawRepresentation);
2425
object raw = new();
@@ -31,5 +32,25 @@ public void Constructor_PropsRoundtrip()
3132
Assert.Same(props, c.AdditionalProperties);
3233
}
3334

34-
private sealed class DerivedAIContent : AIContent;
35+
[Fact]
36+
public void Serialization_Roundtrips()
37+
{
38+
AIContent original = new()
39+
{
40+
RawRepresentation = new object(),
41+
AdditionalProperties = new AdditionalPropertiesDictionary { { "key", "value" } }
42+
};
43+
44+
Assert.NotNull(original.RawRepresentation);
45+
Assert.Single(original.AdditionalProperties);
46+
47+
string json = JsonSerializer.Serialize(original, AIJsonUtilities.DefaultOptions.GetTypeInfo(typeof(AIContent)));
48+
Assert.NotNull(json);
49+
50+
AIContent? deserialized = (AIContent?)JsonSerializer.Deserialize(json, AIJsonUtilities.DefaultOptions.GetTypeInfo(typeof(AIContent)));
51+
Assert.NotNull(deserialized);
52+
Assert.Null(deserialized.RawRepresentation);
53+
Assert.NotNull(deserialized.AdditionalProperties);
54+
Assert.Single(deserialized.AdditionalProperties);
55+
}
3556
}

0 commit comments

Comments
 (0)