convertToModelMessages
Convert useChat messages to ModelMessages for AI functions (API Reference)
The convertToModelMessages function is used to transform an array of UI messages from the useChat hook into an array of ModelMessage objects. These ModelMessage objects are compatible with AI core functions like streamText.
Import
import { convertToModelMessages } from "ai"
API Signature
Parameters
messages:
options:
Returns
A Promise that resolves to an array of ModelMessage objects.
Promise<ModelMessage[]>:
Deprecated rawInput field
Tool parts in the output-error state should store their tool arguments in
input. The legacy rawInput field remains supported for persisted messages,
but convertToModelMessages emits an AI SDK deprecation warning when it
encounters a defined value.
When both fields are present, input takes precedence when it is non-nullish.
For backward compatibility, rawInput remains the fallback when input is
null or undefined. Migrate stored messages to input before the next major
version, when rawInput will be removed.
Tool Approval States
convertToModelMessages preserves tool approval state from UI messages when converting them back into ModelMessages for a follow-up generateText or streamText call.
- Tool parts with approval metadata produce
tool-approval-requestcontent parts.approval.requestReasonis forwarded as the requestreasonwhen present. - Tool parts in
approval-respondedstate also becometool-approval-responsecontent parts. The separate responsereasonis forwarded when present. - Automatic approval metadata is preserved by forwarding
approval.isAutomaticto thetool-approval-requestpart. - Denied tool approvals also produce a synthetic
tool-resultwithoutput: { type: 'execution-denied', reason?: string }, so the model receives a complete tool lifecycle and can respond to the denial in the next step.
Multi-modal Tool Responses
The convertToModelMessages function supports tools that can return multi-modal content. This is useful when tools need to return non-text content like images.
Tools can implement the optional toModelOutput method to transform their results into multi-modal content. The content is an array of content parts, where each part has a type (e.g., 'text', 'image') and corresponding data.
Custom Data Part Conversion
The convertToModelMessages function supports converting custom data parts attached to user messages. This is useful when users need to include additional context (URLs, code files, JSON configs) with their messages.
Basic Usage
By default, data parts in user messages are filtered out during conversion. To include them, provide a convertDataPart callback that transforms data parts into text or file parts that the model can understand:
Use Cases
Attaching URL Content Allow users to attach URLs to their messages, with the content fetched and formatted for the model:
Including Code Files as Context Let users reference code files in their conversations:
Selective Inclusion Only data parts for which you return a text or file model message part are included, all other data parts are ignored.
Type Safety
The generic parameter ensures full type safety for your custom data parts: