Store and render complete chat history
Conversation interfaces
AI Tools · Type Alias
UIMessage is the AI SDK’s complete, UI-facing representation of a message and its application state. It carries identity, role, optional metadata, and typed parts for rendering text, data, and tool interactions.
UIMessage models the complete message state used by an application interface. It includes the message identity and role, optional application metadata, and typed parts for rendering text, data, and tool interactions.
interface UIMessage<
METADATA = unknown,
DATA_PARTS extends UIDataTypes = UIDataTypes,
TOOLS extends UITools = UITools,
> {
id: string;
role: 'system' | 'user' | 'assistant';
metadata?: METADATA;
parts: Array<UIMessagePart<DATA_PARTS, TOOLS>>;
}| Name | Type | Description |
|---|---|---|
METADATA | unknown | Custom type for additional message information; defaults to unknown. |
DATA_PARTS | UIDataTypes | Custom data-part types; constrained by UIDataTypes and defaulting to UIDataTypes. |
TOOLS | UITools | Tool definitions for typed tool interactions; constrained by UITools and defaulting to UITools. |
A UIMessage object with id, role, optional metadata, and typed parts.
import type { UIMessage } from 'ai';
const message: UIMessage = {
id: 'msg-1',
role: 'user',
parts: [{ type: 'text', text: 'Hello!' }],
};
console.log(message.parts);Creates a UIMessage with the required identifier, role, and renderable parts.
Conversation interfaces
Typed application state
Generative UI
Client-side chat state
UIMessage represents the full application-facing message, including metadata, data parts, and contextual information needed by the UI. ModelMessage is instead the model-facing representation.
Follow-up: How would you adapt UIMessage before sending it to a model? Use convertToModelMessages to transform the UIMessage array into the ModelMessage format.
The type accepts METADATA, DATA_PARTS, and TOOLS generic parameters. These let an application type custom metadata, structured data parts, and tool interactions.
Follow-up: What should the defaults be when customization is unnecessary? The documented defaults are unknown for metadata and the standard UIDataTypes and UITools types for the other parameters.
A UIMessage has an id string, a role restricted to system, user, or assistant, optional metadata, and a parts array of UIMessagePart values. Parts are intended for rendering the message in the interface.
Follow-up: Where should tool-specific rendering logic look? It should inspect the typed parts, including tool parts whose names identify the tool.
ModelMessageUIMessagePartUIDataTypesUIToolsInferUIToolsconvertToModelMessages