Skip to content

Commit 6fd8d15

Browse files
committed
Add TextContent.MediaType
1 parent 284b3e3 commit 6fd8d15

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
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;
45
using System.Diagnostics;
56
using System.Diagnostics.CodeAnalysis;
7+
using System.Text.Json.Serialization;
8+
using Microsoft.Shared.Diagnostics;
69

710
namespace Microsoft.Extensions.AI;
811

@@ -13,6 +16,7 @@ namespace Microsoft.Extensions.AI;
1316
public sealed class TextContent : AIContent
1417
{
1518
private string? _text;
19+
private string? _mediaType;
1620

1721
/// <summary>
1822
/// Initializes a new instance of the <see cref="TextContent"/> class.
@@ -33,6 +37,27 @@ public string Text
3337
set => _text = value;
3438
}
3539

40+
/// <summary>Gets or sets the text media type (also known as MIME type) of the content.</summary>
41+
[JsonPropertyOrder(1)]
42+
public string? MediaType
43+
{
44+
get => _mediaType;
45+
set
46+
{
47+
if (value is not null)
48+
{
49+
if (!DataUriParser.IsValidMediaType(value.AsSpan(), ref value))
50+
{
51+
Throw.ArgumentException(nameof(value), "Invalid media type.");
52+
}
53+
54+
// Don't require a "text/" prefix in order to allow for types like "application/typescript".
55+
}
56+
57+
_mediaType = value;
58+
}
59+
}
60+
3661
/// <inheritdoc/>
3762
public override string ToString() => Text;
3863

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatResponseUpdateTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void JsonSerialization_Roundtrips()
116116
new TextContent("text-1"),
117117
new DataContent("http://localhost/image"),
118118
new FunctionCallContent("callId1", "fc1"),
119-
new DataContent("data"u8.ToArray()),
119+
new DataContent("data"u8.ToArray(), "text/plain"),
120120
new TextContent("text-2"),
121121
],
122122
RawRepresentation = new object(),

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,19 @@ public void Constructor_PropsRoundtrip()
4747
Assert.Equal(string.Empty, c.Text);
4848
Assert.Equal(string.Empty, c.ToString());
4949
}
50+
51+
[Fact]
52+
public void MediaType_ValidValue_Roundtrips()
53+
{
54+
TextContent c = new("text");
55+
Assert.Null(c.MediaType);
56+
57+
foreach (string? mediaType in new[] { null, "text/plain", "application/typescript", "text/html", null })
58+
{
59+
c.MediaType = mediaType;
60+
Assert.Equal(mediaType, c.MediaType);
61+
}
62+
63+
Assert.Equal("text", c.Text);
64+
}
5065
}

0 commit comments

Comments
 (0)