UIMessage serves as the source of truth for your application's state, representing the complete message history including metadata, data parts, and all contextual information. In contrast to ModelMessage, which represents the state or context passed to the model, UIMessage contains the full application state needed for UI rendering and client-side functionality.
interface UIMessage< METADATA = unknown, DATA_PARTS extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools,> { /** * A unique identifier for the message. */ id: string; /** * The role of the message. */ role: 'system' | 'user' | 'assistant'; /** * The metadata of the message. */ metadata?: METADATA; /** * The parts of the message. Use this for rendering the message in the UI. */ parts: Array<UIMessagePart<DATA_PARTS, TOOLS>>;}
type ReasoningUIPart = { type: 'reasoning'; /** * The reasoning part ID. */ id?: string; /** * The reasoning text. */ text: string; /** * The state of the reasoning part. */ state?: 'streaming' | 'done'; /** * The provider metadata. */ providerMetadata?: Record<string, any>;};
approval.descriptor contains optional opaque metadata supplied as
approvalDescriptor on the approval request stream chunk. It is preserved when
the tool part transitions from approval-requested to approval-responded and
in later approval-bearing output states.
A static or dynamic tool part whose execution failed. Use the
isToolOutputErrorUIPart type guard when rendering messages so your code does
not need to check the tool state discriminator directly.
import { isToolOutputErrorUIPart, type UIMessage } from 'ai';function ToolError({ part }: { part: UIMessage['parts'][number] }) { if (!isToolOutputErrorUIPart(part)) { return null; } return <div role="alert">{part.errorText}</div>;}
The generic ToolOutputErrorUIPart<TOOLS> type preserves the input types of
static tools and also includes dynamic tool errors:
A provider-specific custom content part of a message.
type CustomContentUIPart = { type: 'custom'; /** * The kind of custom content, in the format `{provider}.{provider-type}`. */ kind: `${string}.${string}`; /** * The provider metadata. */ providerMetadata?: Record<string, any>;};
type FileUIPart = { type: 'file'; /** * IANA media type of the file. */ mediaType: string; /** * Optional filename of the file. */ filename?: string; /** * The URL of the file. * It can either be a URL to a hosted file or a Data URL. */ url: string;};