Skip to content

Commit de6d026

Browse files
.NET: fix parallel tool call rendering in AGUI translation layer (#6009)
Fix three interlocked bugs that prevent parallel tool calls from rendering correctly in AG-UI protocol clients: Bug #1: Scope synthetic MessageId fallback to text events only. The shared streamingMessageId was leaking into ToolCallStartEvent.ParentMessageId, causing all parallel tool calls to collapse into one FE card. Bug #2: Make ToolCallResultEvent.MessageId deterministically unique using result-{CallId} format. MEAI's FunctionInvokingChatClient batches all results with a shared MessageId, collapsing them in FE reconciliation. Bug #3: Coalesce consecutive assistant-tool-call messages in AsChatMessages. Once Bug #1 is fixed, the FE produces separate AGUIAssistantMessage per tool call. On multi-turn replay these become consecutive assistant messages without intervening tool results, triggering HTTP 400 from Azure OpenAI. Remove the now-dead ContainsToolResult helper introduced by PR #5800. Co-authored-by: Copilot <[email protected]>
1 parent 0099a6e commit de6d026

4 files changed

Lines changed: 299 additions & 43 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI.AGUI/Shared/AGUIChatMessageExtensions.cs‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,28 @@ public static IEnumerable<ChatMessage> AsChatMessages(
2020
this IEnumerable<AGUIMessage> aguiMessages,
2121
JsonSerializerOptions jsonSerializerOptions)
2222
{
23+
// Coalesce consecutive AGUIAssistantMessages that carry tool_calls into a single
24+
// ChatMessage. The AG-UI client (e.g. @ag-ui/client) creates a separate assistant
25+
// message per tool call when ToolCallStartEvent.parentMessageId is empty, but
26+
// OpenAI's chat-completion API requires every assistant message with tool_calls
27+
// to be IMMEDIATELY followed by tool responses for each of its tool_call_ids.
28+
// Sending two consecutive single-tool-call assistant messages before any tool
29+
// result triggers HTTP 400 "tool_call_ids did not have response messages".
30+
List<AIContent>? pendingContents = null;
31+
string? pendingId = null;
32+
2333
foreach (var message in aguiMessages)
2434
{
35+
bool isAssistantWithToolCalls =
36+
message is AGUIAssistantMessage am && am.ToolCalls is { Length: > 0 };
37+
38+
if (pendingContents is not null && !isAssistantWithToolCalls)
39+
{
40+
yield return new ChatMessage(ChatRole.Assistant, pendingContents) { MessageId = pendingId };
41+
pendingContents = null;
42+
pendingId = null;
43+
}
44+
2545
var role = MapChatRole(message.Role);
2646

2747
switch (message)
@@ -84,14 +104,14 @@ public static IEnumerable<ChatMessage> AsChatMessages(
84104

85105
case AGUIAssistantMessage assistantMessage when assistantMessage.ToolCalls is { Length: > 0 }:
86106
{
87-
var contents = new List<AIContent>();
107+
pendingContents ??= new List<AIContent>();
108+
pendingId ??= message.Id;
88109

89110
if (!string.IsNullOrEmpty(assistantMessage.Content))
90111
{
91-
contents.Add(new TextContent(assistantMessage.Content));
112+
pendingContents.Add(new TextContent(assistantMessage.Content));
92113
}
93114

94-
// Add tool calls
95115
foreach (var toolCall in assistantMessage.ToolCalls)
96116
{
97117
Dictionary<string, object?>? arguments = null;
@@ -102,16 +122,12 @@ public static IEnumerable<ChatMessage> AsChatMessages(
102122
jsonSerializerOptions.GetTypeInfo(typeof(Dictionary<string, object?>)));
103123
}
104124

105-
contents.Add(new FunctionCallContent(
125+
pendingContents.Add(new FunctionCallContent(
106126
toolCall.Id,
107127
toolCall.Function.Name,
108128
arguments));
109129
}
110130

111-
yield return new ChatMessage(role, contents)
112-
{
113-
MessageId = message.Id
114-
};
115131
break;
116132
}
117133

@@ -134,6 +150,12 @@ public static IEnumerable<ChatMessage> AsChatMessages(
134150
}
135151
}
136152
}
153+
154+
// Flush remaining pending assistant-tool-call entry at end of stream.
155+
if (pendingContents is not null)
156+
{
157+
yield return new ChatMessage(ChatRole.Assistant, pendingContents) { MessageId = pendingId };
158+
}
137159
}
138160

139161
public static IEnumerable<AGUIMessage> AsAGUIMessages(

‎dotnet/src/Microsoft.Agents.AI.AGUI/Shared/ChatResponseUpdateAGUIExtensions.cs‎

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -448,24 +448,36 @@ public static async IAsyncEnumerable<BaseEvent> AsAGUIEventStreamAsync(
448448
};
449449

450450
string? currentMessageId = null;
451-
string? streamingMessageId = null;
451+
string? textStreamingFallback = null;
452+
bool textInFallback = false;
452453
string? currentReasoningBaseId = null;
453454
string? currentReasoningId = null;
454455
string? currentReasoningMessageId = null;
455456
await foreach (var chatResponse in updates.WithCancellation(cancellationToken).ConfigureAwait(false))
456457
{
457-
// Generate a fallback MessageId when the provider doesn't supply one.
458-
// This ensures all AGUI events have a valid messageId regardless of agent type.
459-
if (string.IsNullOrWhiteSpace(chatResponse.MessageId))
458+
// The text-event surface (TextMessageStart/Content/End) requires a non-empty
459+
// MessageId to be valid AGUI. Generate a fallback scoped to a contiguous run of
460+
// null/empty-MessageId chunks (one logical text message). Leave the raw
461+
// chatResponse.MessageId untouched so the tool-call surface below uses the raw
462+
// provider value — collapsing parallel tool calls under a synthetic shared parent
463+
// would make the FE render them as one assistant-message bubble instead of
464+
// distinct rows.
465+
string? textMessageId = chatResponse.MessageId;
466+
if (string.IsNullOrWhiteSpace(textMessageId))
460467
{
461-
chatResponse.MessageId = ContainsToolResult(chatResponse)
462-
? Guid.NewGuid().ToString("N")
463-
: (streamingMessageId ??= Guid.NewGuid().ToString("N"));
468+
textStreamingFallback ??= Guid.NewGuid().ToString("N");
469+
textMessageId = textStreamingFallback;
470+
textInFallback = true;
471+
}
472+
else if (textInFallback)
473+
{
474+
textStreamingFallback = null;
475+
textInFallback = false;
464476
}
465477

466478
if (chatResponse is { Contents.Count: > 0 } &&
467479
chatResponse.Contents[0] is TextContent &&
468-
!string.Equals(currentMessageId, chatResponse.MessageId, StringComparison.Ordinal))
480+
!string.Equals(currentMessageId, textMessageId, StringComparison.Ordinal))
469481
{
470482
// Close any open reasoning block before opening a text message, so AG-UI
471483
// events are properly bracketed. MEAI providers share one MessageId across
@@ -498,11 +510,11 @@ chatResponse.Contents[0] is TextContent &&
498510
// Start the new message
499511
yield return new TextMessageStartEvent
500512
{
501-
MessageId = chatResponse.MessageId!,
513+
MessageId = textMessageId!,
502514
Role = chatResponse.Role!.Value.Value
503515
};
504516

505-
currentMessageId = chatResponse.MessageId;
517+
currentMessageId = textMessageId;
506518
}
507519

508520
// Emit text content if present
@@ -577,9 +589,15 @@ chatResponse.Contents[0] is TextContent &&
577589
currentReasoningMessageId = null;
578590
}
579591

592+
// Each tool result is a distinct tool-role message on the AGUI wire.
593+
// MEAI's FunctionInvokingChatClient shares one synthetic MessageId
594+
// across all FunctionResultContent items, but the FE keys messages
595+
// by id, so emitting them with the same id collapses them in React
596+
// reconciliation. Derive a unique, deterministic per-result id from
597+
// the (LLM-assigned) call id.
580598
yield return new ToolCallResultEvent
581599
{
582-
MessageId = chatResponse.MessageId,
600+
MessageId = $"result-{functionResultContent.CallId}",
583601
ToolCallId = functionResultContent.CallId,
584602
Content = SerializeResultContent(functionResultContent, jsonSerializerOptions) ?? "",
585603
Role = AGUIRoles.Tool
@@ -674,7 +692,7 @@ chatResponse.Contents[0] is TextContent &&
674692
// Text content event
675693
yield return new TextMessageContentEvent
676694
{
677-
MessageId = chatResponse.MessageId!,
695+
MessageId = textMessageId!,
678696
#if !NET
679697
Delta = Encoding.UTF8.GetString(dataContent.Data.ToArray())
680698
#else
@@ -726,17 +744,4 @@ chatResponse.Contents[0] is TextContent &&
726744
_ => JsonSerializer.Serialize(functionResultContent.Result, options.GetTypeInfo(functionResultContent.Result.GetType())),
727745
};
728746
}
729-
730-
private static bool ContainsToolResult(ChatResponseUpdate chatResponse)
731-
{
732-
foreach (AIContent content in chatResponse.Contents)
733-
{
734-
if (content is FunctionResultContent)
735-
{
736-
return true;
737-
}
738-
}
739-
740-
return false;
741-
}
742747
}

‎dotnet/tests/Microsoft.Agents.AI.AGUI.UnitTests/AGUIChatMessageExtensionsTests.cs‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,4 +914,147 @@ public void AsAGUIMessages_WithDictionaryContainingCustomTypes_SerializesDirectl
914914
}
915915

916916
#endregion
917+
918+
#region Consecutive Assistant-Tool-Call Coalescing
919+
920+
/// <summary>
921+
/// Bug #3 reproduction: consecutive AGUIAssistantMessages with ToolCalls should
922+
/// be coalesced into a single ChatMessage with multiple FunctionCallContent
923+
/// entries. Without coalescing, Azure OpenAI rejects the history with HTTP 400.
924+
/// </summary>
925+
[Fact]
926+
public void AsChatMessages_ConsecutiveAssistantToolCallMessages_CoalesceIntoOneChatMessage()
927+
{
928+
// Arrange — 3 consecutive assistant messages with tool calls (no intervening tool msg)
929+
List<AGUIMessage> aguiMessages =
930+
[
931+
new AGUIUserMessage { Id = "user-1", Content = "Run 3 queries" },
932+
new AGUIAssistantMessage
933+
{
934+
Id = "asst-1",
935+
Content = "",
936+
ToolCalls =
937+
[
938+
new AGUIToolCall { Id = "call_A", Type = "function", Function = new AGUIFunctionCall { Name = "query", Arguments = "{\"q\":\"1\"}" } }
939+
]
940+
},
941+
new AGUIAssistantMessage
942+
{
943+
Id = "asst-2",
944+
Content = "",
945+
ToolCalls =
946+
[
947+
new AGUIToolCall { Id = "call_B", Type = "function", Function = new AGUIFunctionCall { Name = "query", Arguments = "{\"q\":\"2\"}" } }
948+
]
949+
},
950+
new AGUIAssistantMessage
951+
{
952+
Id = "asst-3",
953+
Content = "",
954+
ToolCalls =
955+
[
956+
new AGUIToolCall { Id = "call_C", Type = "function", Function = new AGUIFunctionCall { Name = "query", Arguments = "{\"q\":\"3\"}" } }
957+
]
958+
},
959+
new AGUIToolMessage { Id = "tool-1", ToolCallId = "call_A", Content = "\"result1\"" },
960+
new AGUIToolMessage { Id = "tool-2", ToolCallId = "call_B", Content = "\"result2\"" },
961+
new AGUIToolMessage { Id = "tool-3", ToolCallId = "call_C", Content = "\"result3\"" },
962+
new AGUIUserMessage { Id = "user-2", Content = "Run it again" },
963+
];
964+
965+
// Act
966+
List<ChatMessage> chatMessages = aguiMessages.AsChatMessages(AGUIJsonSerializerContext.Default.Options).ToList();
967+
968+
// Assert — the 3 consecutive assistant-tool-call messages should coalesce into 1
969+
List<ChatMessage> assistantWithToolCalls = chatMessages
970+
.Where(m => m.Role == ChatRole.Assistant && m.Contents.OfType<FunctionCallContent>().Any())
971+
.ToList();
972+
973+
Assert.Single(assistantWithToolCalls);
974+
975+
// The single coalesced message should contain all 3 FunctionCallContent entries
976+
List<FunctionCallContent> functionCalls = assistantWithToolCalls[0].Contents
977+
.OfType<FunctionCallContent>().ToList();
978+
Assert.Equal(3, functionCalls.Count);
979+
Assert.Equal("call_A", functionCalls[0].CallId);
980+
Assert.Equal("call_B", functionCalls[1].CallId);
981+
Assert.Equal("call_C", functionCalls[2].CallId);
982+
983+
// MessageId should be from the first message in the coalesced group
984+
Assert.Equal("asst-1", assistantWithToolCalls[0].MessageId);
985+
986+
// Total messages: user + coalesced assistant + 3 tools + user = 6
987+
Assert.Equal(6, chatMessages.Count);
988+
}
989+
990+
/// <summary>
991+
/// A single assistant message with tool calls (not consecutive) should still
992+
/// produce one ChatMessage — no behavior change from coalescing logic.
993+
/// </summary>
994+
[Fact]
995+
public void AsChatMessages_SingleAssistantToolCallMessage_ProducesOneChatMessage()
996+
{
997+
// Arrange
998+
List<AGUIMessage> aguiMessages =
999+
[
1000+
new AGUIAssistantMessage
1001+
{
1002+
Id = "asst-1",
1003+
Content = "Here are the results",
1004+
ToolCalls =
1005+
[
1006+
new AGUIToolCall { Id = "call_A", Type = "function", Function = new AGUIFunctionCall { Name = "query", Arguments = "{}" } },
1007+
new AGUIToolCall { Id = "call_B", Type = "function", Function = new AGUIFunctionCall { Name = "query", Arguments = "{}" } },
1008+
]
1009+
},
1010+
new AGUIToolMessage { Id = "tool-1", ToolCallId = "call_A", Content = "\"r1\"" },
1011+
new AGUIToolMessage { Id = "tool-2", ToolCallId = "call_B", Content = "\"r2\"" },
1012+
];
1013+
1014+
// Act
1015+
List<ChatMessage> chatMessages = aguiMessages.AsChatMessages(AGUIJsonSerializerContext.Default.Options).ToList();
1016+
1017+
// Assert — single assistant message, not coalesced from multiple
1018+
Assert.Equal(3, chatMessages.Count);
1019+
Assert.Equal(ChatRole.Assistant, chatMessages[0].Role);
1020+
List<FunctionCallContent> calls = chatMessages[0].Contents.OfType<FunctionCallContent>().ToList();
1021+
Assert.Equal(2, calls.Count);
1022+
Assert.Equal("asst-1", chatMessages[0].MessageId);
1023+
}
1024+
1025+
/// <summary>
1026+
/// When consecutive assistant-tool-call messages are at the END of the stream
1027+
/// (no subsequent non-tool-call message to trigger flush), they should still
1028+
/// be coalesced and flushed.
1029+
/// </summary>
1030+
[Fact]
1031+
public void AsChatMessages_ConsecutiveAssistantToolCallsAtEndOfStream_FlushesCorrectly()
1032+
{
1033+
// Arrange — stream ends with consecutive assistant tool-call messages
1034+
List<AGUIMessage> aguiMessages =
1035+
[
1036+
new AGUIUserMessage { Id = "user-1", Content = "Do things" },
1037+
new AGUIAssistantMessage
1038+
{
1039+
Id = "asst-1",
1040+
ToolCalls = [new AGUIToolCall { Id = "call_X", Type = "function", Function = new AGUIFunctionCall { Name = "fn", Arguments = "{}" } }]
1041+
},
1042+
new AGUIAssistantMessage
1043+
{
1044+
Id = "asst-2",
1045+
ToolCalls = [new AGUIToolCall { Id = "call_Y", Type = "function", Function = new AGUIFunctionCall { Name = "fn", Arguments = "{}" } }]
1046+
},
1047+
];
1048+
1049+
// Act
1050+
List<ChatMessage> chatMessages = aguiMessages.AsChatMessages(AGUIJsonSerializerContext.Default.Options).ToList();
1051+
1052+
// Assert — should be user + 1 coalesced assistant = 2 messages
1053+
Assert.Equal(2, chatMessages.Count);
1054+
Assert.Equal(ChatRole.User, chatMessages[0].Role);
1055+
Assert.Equal(ChatRole.Assistant, chatMessages[1].Role);
1056+
Assert.Equal(2, chatMessages[1].Contents.OfType<FunctionCallContent>().Count());
1057+
}
1058+
1059+
#endregion
9171060
}

0 commit comments

Comments
 (0)