Skip to content

Commit 19b5db8

Browse files
Shyam Namboodiripadjeffhandley
authored andcommitted
Merged PR 49636: [9.4.3] [cherry-pick] Skip messages that have no text when rendering conversations as part of evaluation prompts (#6349)
1 parent e9b8ef0 commit 19b5db8

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/Libraries/Microsoft.Extensions.AI.Evaluation/ChatMessageExtensions.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ conversationHistory[lastMessageIndex] is ChatMessage lastMessage &&
9797
/// <remarks>
9898
/// <para>
9999
/// This function only considers the <see cref="ChatMessage.Text"/> and ignores any <see cref="AIContent"/>s
100-
/// present within the <see cref="ChatMessage.Contents"/> of the <paramref name="message"/> that are not
101-
/// <see cref="TextContent"/>s.
100+
/// (present within the <see cref="ChatMessage.Contents"/> of the <paramref name="message"/>) that are not
101+
/// <see cref="TextContent"/>s. If the <paramref name="message"/> does not contain any <see cref="TextContent"/>s
102+
/// then this function returns an empty string.
102103
/// </para>
103104
/// <para>
104105
/// The returned string is prefixed with the <see cref="ChatMessage.Role"/> and
@@ -112,6 +113,12 @@ public static string RenderText(this ChatMessage message)
112113
{
113114
_ = Throw.IfNull(message);
114115

116+
if (!message.Contents.OfType<TextContent>().Any())
117+
{
118+
// Don't render messages (such as messages with role ChatRole.Tool) that don't contain any textual content.
119+
return string.Empty;
120+
}
121+
115122
string? author = message.AuthorName;
116123
string role = message.Role.Value;
117124
string? content = message.Text;
@@ -129,8 +136,10 @@ public static string RenderText(this ChatMessage message)
129136
/// <remarks>
130137
/// <para>
131138
/// This function only considers the <see cref="ChatMessage.Text"/> and ignores any <see cref="AIContent"/>s
132-
/// present within the <see cref="ChatMessage.Contents"/> of the <paramref name="messages"/> that are not
133-
/// <see cref="TextContent"/>s.
139+
/// (present within the <see cref="ChatMessage.Contents"/> of the <paramref name="messages"/>) that are not
140+
/// <see cref="TextContent"/>s. Any <paramref name="messages"/> that contain no <see cref="TextContent"/>s will be
141+
/// skipped and will not be rendered. If none of the <paramref name="messages"/> include any
142+
/// <see cref="TextContent"/>s then this function will return an empty string.
134143
/// </para>
135144
/// <para>
136145
/// The rendered <paramref name="messages"/> are each prefixed with the <see cref="ChatMessage.Role"/> and

src/Libraries/Microsoft.Extensions.AI.Evaluation/ChatResponseExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public static class ChatResponseExtensions
1919
/// <para>
2020
/// This function only considers the <see cref="ChatResponse.Text"/> and ignores any <see cref="AIContent"/>s
2121
/// (present within the <see cref="ChatMessage.Contents"/> of the <see cref="ChatResponse.Messages"/>) that are not
22-
/// <see cref="TextContent"/>s.
22+
/// <see cref="TextContent"/>s. Any <see cref="ChatResponse.Messages"/> that contain no <see cref="TextContent"/>s
23+
/// will be skipped and will not be rendered. If none of the <see cref="ChatResponse.Messages"/> include any
24+
/// <see cref="TextContent"/>s then this function will return an empty string.
2325
/// </para>
2426
/// <para>
2527
/// The rendered <see cref="ChatResponse.Messages"/> are each prefixed with the <see cref="ChatMessage.Role"/> and

0 commit comments

Comments
 (0)