diff --git a/dotnet/src/SemanticKernel.Abstractions/Contents/ChatMessageContent.cs b/dotnet/src/SemanticKernel.Abstractions/Contents/ChatMessageContent.cs
index 24ff3cf19438..0042cf5a2948 100644
--- a/dotnet/src/SemanticKernel.Abstractions/Contents/ChatMessageContent.cs
+++ b/dotnet/src/SemanticKernel.Abstractions/Contents/ChatMessageContent.cs
@@ -20,7 +20,11 @@ public class ChatMessageContent : KernelContent
///
[Experimental("SKEXP0001")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
- public string? AuthorName { get; set; }
+ public string? AuthorName
+ {
+ get => this._authorName;
+ set => this._authorName = string.IsNullOrWhiteSpace(value) ? null : value;
+ }
///
/// Role of the author of the message
@@ -171,4 +175,5 @@ public override string ToString()
private ChatMessageContentItemCollection? _items;
private Encoding _encoding;
+ private string? _authorName;
}
diff --git a/dotnet/src/SemanticKernel.Abstractions/Contents/StreamingChatMessageContent.cs b/dotnet/src/SemanticKernel.Abstractions/Contents/StreamingChatMessageContent.cs
index 5cc7afb582ed..cc2b0c354284 100644
--- a/dotnet/src/SemanticKernel.Abstractions/Contents/StreamingChatMessageContent.cs
+++ b/dotnet/src/SemanticKernel.Abstractions/Contents/StreamingChatMessageContent.cs
@@ -64,7 +64,11 @@ public StreamingKernelContentItemCollection Items
///
[Experimental("SKEXP0001")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
- public string? AuthorName { get; set; }
+ public string? AuthorName
+ {
+ get => this._authorName;
+ set => this._authorName = string.IsNullOrWhiteSpace(value) ? null : value;
+ }
///
/// Role of the author of the message
@@ -126,4 +130,5 @@ public StreamingChatMessageContent(AuthorRole? role, string? content, object? in
private StreamingKernelContentItemCollection? _items;
private Encoding _encoding;
+ private string? _authorName;
}
diff --git a/dotnet/src/SemanticKernel.UnitTests/Contents/ChatMessageContentTests.cs b/dotnet/src/SemanticKernel.UnitTests/Contents/ChatMessageContentTests.cs
index fdbd4cae0524..759fec2b532b 100644
--- a/dotnet/src/SemanticKernel.UnitTests/Contents/ChatMessageContentTests.cs
+++ b/dotnet/src/SemanticKernel.UnitTests/Contents/ChatMessageContentTests.cs
@@ -128,6 +128,25 @@ public void ContentPropertyGetterShouldReturnContentOfTheFirstTextContentItem()
Assert.Equal("fake-content-1", sut.Content);
}
+ [Theory]
+ [InlineData(null)]
+ [InlineData("")]
+ [InlineData(" ")]
+ [InlineData("\t")]
+ [InlineData("\n")]
+ [InlineData("\r\n")]
+ public void ContentPropertySetterShouldConvertEmptyOrWhitespaceAuthorNameToNull(string? authorName)
+ {
+ // Arrange
+ var message = new ChatMessageContent(AuthorRole.User, content: null)
+ {
+ AuthorName = authorName
+ };
+
+ // Assert
+ Assert.Null(message.AuthorName);
+ }
+
[Fact]
public void ItShouldBePossibleToSetAndGetEncodingEvenIfThereAreNoItems()
{
diff --git a/dotnet/src/SemanticKernel.UnitTests/Contents/StreamingChatMessageContentTests.cs b/dotnet/src/SemanticKernel.UnitTests/Contents/StreamingChatMessageContentTests.cs
index f7f7c5e43be7..d510e6e42eeb 100644
--- a/dotnet/src/SemanticKernel.UnitTests/Contents/StreamingChatMessageContentTests.cs
+++ b/dotnet/src/SemanticKernel.UnitTests/Contents/StreamingChatMessageContentTests.cs
@@ -118,6 +118,25 @@ public void ContentPropertyGetterShouldReturnContentOfTheFirstTextContentItem()
Assert.Equal("fake-content-1", sut.Content);
}
+ [Theory]
+ [InlineData(null)]
+ [InlineData("")]
+ [InlineData(" ")]
+ [InlineData("\t")]
+ [InlineData("\n")]
+ [InlineData("\r\n")]
+ public void ContentPropertySetterShouldConvertEmptyOrWhitespaceAuthorNameToNull(string? authorName)
+ {
+ // Arrange
+ var message = new StreamingChatMessageContent(AuthorRole.User, content: null)
+ {
+ AuthorName = authorName
+ };
+
+ // Assert
+ Assert.Null(message.AuthorName);
+ }
+
[Fact]
public void ItShouldBePossibleToSetAndGetEncodingEvenIfThereAreNoItems()
{