Migrating to Message Parts
Learn how to migrate to the new message parts property in the useChat hook
The release of @ai-sdk/react@1.1.10
introduced a new property called parts
to messages in the useChat
hook. We recommend rendering the messages using the parts
property instead of the content
property. The parts property supports different message types, including text, tool invocation, and tool result, and allows for more flexible and complex chat and agent-like user interfaces.
You can read the API reference for the parts
property here.
Migrating your existing project to use the parts
property
Your existing project must already have messages stored in the database. To migrate your messages to use parts
, you will have to create new tables for Message
and Vote
, backfill the new tables with transformed messages, and delete (optional) the old tables.
These are the following steps:
- Create tables
Message_v2
andVote_v2
with updated schemas at/lib/db/schema.ts
- Update the
Message
component at/components/message.tsx
to use parts and render content. - Run migration script at
src/lib/db/helpers/01-migrate-to-parts.ts
1. Creating Tables with Updated Schemas
You will mark the earlier tables as deprecated and create two new tables, Message_v2
and Vote_v2
. Table Message_v2
contains the updated schema that includes the parts
property. Table Vote_v2
does not contain schema changes but will contain votes that point to the transformed messages in Message_v2
.
Before creating the new tables, you will need to update the variables of existing tables to indicate that they are deprecated.
After deprecating the current table schemas, you can now proceed to create schemas for the new tables.
2. Updating the Message Component
Previously you were using content types to render messages and tool invocations. Now you will use the parts
property to render messages and tool invocations.
3. Running the Migration Script
At this point, you can deploy your application so new messages can be stored in the new format.
To restore messages of previous chat conversations, you can run the following script that applies a transformation to the old messages and stores them in the new format.
This script will take some time to complete based on the number of messages to be migrated. After completion, you can verify that the messages have been successfully migrated by checking the database.