Open History to see the last commit
1---
2id: uiMessageToModelMessages
3title: uiMessageToModelMessages
4---
5
6```ts
7function uiMessageToModelMessages(uiMessage): ModelMessage<
8 | string
9 | ContentPart<unknown, unknown, unknown, unknown, unknown>[]
10 | null>[];
11```
12
13Defined in: [packages/ai/src/activities/chat/messages.ts:576](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/messages.ts#L576)
14
15Convert a UIMessage to ModelMessage(s)
16
17Walks the parts array IN ORDER to preserve the interleaving of text,
18tool calls, and tool results. This is critical for multi-round tool
19flows where the model generates text, calls a tool, gets the result,
20then generates more text and calls another tool.
21
22The output preserves the sequential structure:
23 text1 → toolCall1 → toolResult1 → text2 → toolCall2 → toolResult2
24becomes:
25 assistant: {content: "text1", toolCalls: [toolCall1]}
26 tool: toolResult1
27 assistant: {content: "text2", toolCalls: [toolCall2]}
28 tool: toolResult2
29
30## Parameters
31
32### uiMessage
33
34[`UIMessage`](../interfaces/UIMessage.md)
35
36The UIMessage to convert
37
38## Returns
39
40[`ModelMessage`](../interfaces/ModelMessage.md)\<
41 \| `string`
42 \| [`ContentPart`](../type-aliases/ContentPart.md)\<`unknown`, `unknown`, `unknown`, `unknown`, `unknown`\>[]
43 \| `null`\>[]
44
45An array of ModelMessages preserving part ordering