content is a string or array:Overview
The message types in ff-ai extend the Vercel AI SDK’sModelMessage with additional metadata for persistence and identification.
ConversationMessage
TheConversationMessage namespace provides types and utilities for working with conversation messages.
Type Definition
Message Structure
AConversationMessage includes:
UUID v7 identifier for the message. Generated automatically using the
uuid package.Timestamp when the message was created. Used for ordering messages chronologically.
The role of the message sender (inherited from AI SDK)
The message content. Can be a simple string or an array of content parts (text, images, tool calls, etc.)
Creating Messages
Messages are typically created automatically when usingcreateTurnHandler. The turn handler’s saveUserMessage and onStep methods handle message creation internally.
If you need to manually create a message (e.g., for custom storage implementations), construct it with the required fields:
Most users should rely on
createTurnHandler to manage message creation automatically rather than creating messages manually.Converting Messages
convertToUIMessage
Convert aConversationMessage to the AI SDK’s UIMessage format for rendering in user interfaces:
- Extracts text parts from the message content
- Maps
toolrole toassistant(UI convention) - Preserves message ID for React keys
- Returns an array of
UIMessagePartobjects
Message Roles
Messages can have different roles based on their source:user
user
Messages sent by the end user. These are the primary input to the AI model.
assistant
assistant
Messages generated by the AI model. Can include text responses or tool calls.
tool
tool
Results from tool executions. These provide context back to the model.
system
system
System instructions that guide the model’s behavior. Not stored in conversation history by default.
Content Types
Messages can contain different types of content:Text Content
Simple string content:Structured Content
Array of content parts for complex messages:Tool Calls
Assistant messages requesting tool execution:Working with Messages
Complete Example
Multi-part Messages
Tool Result Messages
Message IDs
Message IDs use UUID v7, which provides:- Time-ordered: IDs are sortable by creation time
- Unique: Globally unique across distributed systems
- Performance: Efficient for database indexing
Type Safety
TheConversationMessage.Id type is branded using Valibot:
Best Practices
Always use fromModelMessage
Always use fromModelMessage
Don’t manually construct
ConversationMessage objects. Use fromModelMessage to ensure IDs and timestamps are properly generated:Use convertToUIMessage for rendering
Use convertToUIMessage for rendering
When displaying messages in a UI, always convert them first:
Preserve message history
Preserve message history
Store complete messages including metadata. Don’t discard
id and createdAt as they’re needed for message ordering and deduplication.Handle multi-part content
Handle multi-part content
Always check if
Next Steps
Turn Handler
Automatically manage message creation and persistence
Conversation Store
Store and retrieve conversation messages
Examples
See complete examples with messages