fix(ag-ui): preserve ModelRequest part order in _dump_request_parts

Closed
TC
5mo ago
+8-0

Summary

Fixes #5964 — AGUIAdapter.dump_messages() reorders messages when a ModelRequest contains both a ToolReturnPart and a UserPromptPart.

Problem

_dump_request_parts buffered all UserPromptPart content in a separate list and flushed it at the end, before the already-accumulated tool returns. This always placed user messages before tool messages, regardless of the original part order.

For providers like Anthropic/Bedrock, this causes a 400 error because the tool_use block appears without an immediately following tool_result.

Fix

Flush buffered user content when encountering a ToolReturnPart during iteration, preserving the original ModelRequest.parts order. The existing user-content combining logic (consecutive UserPromptParts → single UserMessage) is unchanged.

Test Plan

The MRE from #5964 now passes — [ToolReturnPart, UserPromptPart] dumps as [ToolMessage, UserMessage] in the correct order.

Closes #5964