Skip to content

Commit 4e82b0b

Browse files
committed
Fix assistant messages conversions in OpenAiResponseAgent chat history
1 parent dc7c1c0 commit 4e82b0b

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

dotnet/src/Agents/OpenAI/Extensions/ChatContentMessageExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ public static IEnumerable<ThreadInitializationMessage> ToThreadInitializationMes
4444
public static ResponseItem ToResponseItem(this ChatMessageContent message)
4545
{
4646
var items = message.Items;
47-
IEnumerable<ResponseContentPart> contentParts = items.Select(item => item.ToResponseContentPart());
48-
return message.Role.Label.ToUpperInvariant() switch
47+
var roleLabel = message.Role.Label.ToUpperInvariant();
48+
49+
IEnumerable<ResponseContentPart> contentParts = items.Select(item => item.ToResponseContentPart(roleLabel == "ASSISTANT"));
50+
return roleLabel switch
4951
{
5052
"SYSTEM" => ResponseItem.CreateSystemMessageItem(contentParts),
5153
"USER" => ResponseItem.CreateUserMessageItem(contentParts),

dotnet/src/Agents/OpenAI/Extensions/KernelContentExtensions.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,23 @@ namespace Microsoft.SemanticKernel.Agents.OpenAI;
1010
/// </summary>
1111
internal static class KernelContentExtensions
1212
{
13-
internal static ResponseContentPart ToResponseContentPart(this KernelContent content)
13+
internal static ResponseContentPart ToResponseContentPart(this KernelContent content, bool isOutput = false)
1414
{
1515
return content switch
1616
{
17-
TextContent textContent => textContent.ToResponseContentPart(),
17+
TextContent textContent => textContent.ToResponseContentPart(isOutput),
1818
ImageContent imageContent => imageContent.ToResponseContentPart(),
1919
BinaryContent binaryContent => binaryContent.ToResponseContentPart(),
2020
FileReferenceContent fileReferenceContent => fileReferenceContent.ToResponseContentPart(),
2121
_ => throw new NotSupportedException($"Unsupported content type {content.GetType().Name}. Cannot convert to {nameof(ResponseContentPart)}.")
2222
};
2323
}
2424

25-
internal static ResponseContentPart ToResponseContentPart(this TextContent content)
25+
internal static ResponseContentPart ToResponseContentPart(this TextContent content, bool isOutput)
2626
{
27-
return ResponseContentPart.CreateInputTextPart(content.Text);
27+
return isOutput ?
28+
ResponseContentPart.CreateOutputTextPart(content.Text, []) :
29+
ResponseContentPart.CreateInputTextPart(content.Text);
2830
}
2931

3032
internal static ResponseContentPart ToResponseContentPart(this ImageContent content)

0 commit comments

Comments
 (0)