Skip to content

Commit 8ea6f83

Browse files
Python: fix: preserve parallel function_result contents in AG-UI conversion (microsoft#7980)
* Python: fix: preserve parallel function_result contents in AG-UI conversion agent_framework_messages_to_agui collapsed a tool Message carrying multiple function_result contents (parallel tool calls) into a single AG-UI tool message, keeping only the last call_id/result. Clients then saw the missing results as pending tool approvals. Emit one AG-UI tool message per function_result, each with its own toolCallId and content, with distinct ids so downstream clients keyed by id do not re-collapse them. Fixes microsoft#7979 * Python: fix: preserve non-result contents in mixed AG-UI tool messages A single Agent Framework message can carry function_result contents alongside text or function_call contents (e.g. a finalized assistant turn). The parallel-result fix emitted the tool messages then returned early, dropping that leftover content. Emit a separate, distinctly-identified AG-UI message for any remaining text/tool_calls after the split tool results, so mixed messages are preserved instead of silently losing the assistant output. * Python: fix: use independent ids for split AG-UI tool messages Deriving suffixes from the source id (f"{base_id}-{idx}") could collide with a legitimate message id elsewhere in the converted history, letting id-keyed clients re-collapse a result. Keep the source id for the first result and generate an independent id for each additional message.
1 parent e2f7db2 commit 8ea6f83

2 files changed

Lines changed: 100 additions & 10 deletions

File tree

‎python/packages/ag-ui/agent_framework_ag_ui/_message_adapters.py‎

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ def agent_framework_messages_to_agui(messages: list[Message] | list[dict[str, An
995995

996996
content_text = ""
997997
tool_calls: list[dict[str, Any]] = []
998-
tool_result_call_id: str | None = None
998+
function_results: list[Any] = []
999999

10001000
for content in msg.contents:
10011001
if content.type == "text":
@@ -1012,9 +1012,38 @@ def agent_framework_messages_to_agui(messages: list[Message] | list[dict[str, An
10121012
}
10131013
)
10141014
elif content.type == "function_result":
1015-
# Tool result content - extract call_id and result
1016-
tool_result_call_id = content.call_id
1017-
content_text = content.result if content.result is not None else ""
1015+
function_results.append(content)
1016+
1017+
# A single Agent Framework message can carry several function_result
1018+
# contents (parallel tool calls). Emit one AG-UI tool message per result so
1019+
# none are dropped and each keeps its own toolCallId.
1020+
if function_results:
1021+
# Preserve the source id for the first result; give every additional
1022+
# message an independent generated id. Deriving suffixes from the source
1023+
# id (e.g. f"{base_id}-1") risks colliding with a legitimate id elsewhere
1024+
# in the history, which would let id-keyed clients re-collapse results.
1025+
for idx, fr in enumerate(function_results):
1026+
result.append(
1027+
{
1028+
"id": msg.message_id if (idx == 0 and msg.message_id) else generate_event_id(),
1029+
"role": "tool",
1030+
"content": fr.result if fr.result is not None else "",
1031+
"toolCallId": fr.call_id,
1032+
}
1033+
)
1034+
# A mixed message may also carry text / function_call contents alongside
1035+
# the tool results (e.g. a finalized assistant turn). Emit those as a
1036+
# separate, distinctly-identified message so they are not lost.
1037+
if content_text or tool_calls:
1038+
extra_msg: dict[str, Any] = {
1039+
"id": generate_event_id(),
1040+
"role": role,
1041+
"content": content_text,
1042+
}
1043+
if tool_calls:
1044+
extra_msg["tool_calls"] = tool_calls
1045+
result.append(extra_msg)
1046+
continue
10181047

10191048
agui_msg: dict[str, Any] = {
10201049
"id": msg.message_id if msg.message_id else generate_event_id(), # Always include id
@@ -1025,12 +1054,6 @@ def agent_framework_messages_to_agui(messages: list[Message] | list[dict[str, An
10251054
if tool_calls:
10261055
agui_msg["tool_calls"] = tool_calls
10271056

1028-
# If this is a tool result message, add toolCallId (using camelCase for Pydantic)
1029-
if tool_result_call_id:
1030-
agui_msg["toolCallId"] = tool_result_call_id
1031-
# Tool result messages should have role="tool"
1032-
agui_msg["role"] = "tool"
1033-
10341057
result.append(agui_msg)
10351058

10361059
return result

‎python/packages/ag-ui/tests/ag_ui/test_message_adapters.py‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,73 @@ def test_agent_framework_to_agui_function_result_multiple_text_contents():
896896
assert agui_msg["content"] == '["First result", "Second result"]'
897897

898898

899+
def test_agent_framework_to_agui_parallel_function_results_not_collapsed():
900+
"""A tool Message with several function_result contents emits one AG-UI tool message each."""
901+
msg = Message(
902+
role="tool",
903+
contents=[
904+
Content.from_function_result(call_id="call_a", result="result-a"),
905+
Content.from_function_result(call_id="call_b", result="result-b"),
906+
Content.from_function_result(call_id="call_c", result="result-c"),
907+
],
908+
message_id="tool-batch-1",
909+
)
910+
911+
messages = agent_framework_messages_to_agui([msg])
912+
913+
tool_msgs = [m for m in messages if m.get("role") == "tool"]
914+
assert len(tool_msgs) == 3
915+
assert [m["toolCallId"] for m in tool_msgs] == ["call_a", "call_b", "call_c"]
916+
assert [m["content"] for m in tool_msgs] == ["result-a", "result-b", "result-c"]
917+
# Split messages must carry distinct ids so downstream clients keyed by id do not re-collapse them.
918+
assert len({m["id"] for m in tool_msgs}) == 3
919+
# The first result keeps the source message id for continuity.
920+
assert tool_msgs[0]["id"] == "tool-batch-1"
921+
922+
923+
def test_agent_framework_to_agui_parallel_function_results_none_result():
924+
"""A None result among parallel results maps to empty string without dropping the entry."""
925+
msg = Message(
926+
role="tool",
927+
contents=[
928+
Content.from_function_result(call_id="call_a", result="result-a"),
929+
Content.from_function_result(call_id="call_b", result=None),
930+
],
931+
message_id="tool-batch-2",
932+
)
933+
934+
messages = agent_framework_messages_to_agui([msg])
935+
936+
tool_msgs = [m for m in messages if m.get("role") == "tool"]
937+
assert len(tool_msgs) == 2
938+
assert tool_msgs[1]["toolCallId"] == "call_b"
939+
assert tool_msgs[1]["content"] == ""
940+
941+
942+
def test_agent_framework_to_agui_function_result_with_text_preserves_both():
943+
"""A mixed message (function_result + text) emits a tool message and keeps the text."""
944+
msg = Message(
945+
role="assistant",
946+
contents=[
947+
Content.from_function_result(call_id="weather-call", result="Sunny in Seattle"),
948+
Content.from_text("The weather is sunny."),
949+
],
950+
message_id="mixed-1",
951+
)
952+
953+
messages = agent_framework_messages_to_agui([msg])
954+
955+
assert len(messages) == 2
956+
tool_msg, text_msg = messages
957+
assert tool_msg["role"] == "tool"
958+
assert tool_msg["toolCallId"] == "weather-call"
959+
assert tool_msg["content"] == "Sunny in Seattle"
960+
# The assistant text is not swallowed by the tool result and keeps its own role/id.
961+
assert text_msg["role"] == "assistant"
962+
assert text_msg["content"] == "The weather is sunny."
963+
assert text_msg["id"] != tool_msg["id"]
964+
965+
899966
# Additional tests for better coverage
900967

901968

0 commit comments

Comments
 (0)