0
Fork 0
mirror of https://github.com/TanStack/ai.git synced 2026-11-18 19:34:58 +00:00
ai/docs/reference/functions/uiMessageToModelMessages.md
github-actions[bot] 1999af2267
docs: regenerate API documentation (#1260)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2026-08-28 11:42:19 +02:00

1.3 KiB

id title
uiMessageToModelMessages uiMessageToModelMessages

Function: uiMessageToModelMessages()

function uiMessageToModelMessages(uiMessage): ModelMessage<
  | string
  | ContentPart<unknown, unknown, unknown, unknown, unknown>[]
  | null>[];

Defined in: packages/ai/src/activities/chat/messages.ts:425

Convert a UIMessage to ModelMessage(s)

Walks the parts array IN ORDER to preserve the interleaving of text, tool calls, and tool results. This is critical for multi-round tool flows where the model generates text, calls a tool, gets the result, then generates more text and calls another tool.

The output preserves the sequential structure: text1 → toolCall1 → toolResult1 → text2 → toolCall2 → toolResult2 becomes: assistant: {content: "text1", toolCalls: [toolCall1]} tool: toolResult1 assistant: {content: "text2", toolCalls: [toolCall2]} tool: toolResult2

Parameters

uiMessage

UIMessage

The UIMessage to convert

Returns

ModelMessage< | string | ContentPart<unknown, unknown, unknown, unknown, unknown>[] | null>[]

An array of ModelMessages preserving part ordering