Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

uiMessageToModelMessages.md

47 lines1.3 KB

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