toAiMessages
Convert an array of Message objects into the { role, content }[] format expected by the AI SDK. The output is structurally compatible with AI SDK's ModelMessage[].
Using TanStack AI? toTanStackMessages in chat/ai/tanstack converts the
same history into the ModelMessage[] shape that chat() expects. See
TanStack AI.
toAiMessages is also re-exported from the main chat entrypoint
for backwards compatibility and marked @deprecated in JSDoc. New
code should import it from chat/ai alongside
createChatTools and the rest of the AI
utilities.
Usage
Signature
Parameters
| Prop | Type | Default | Description |
|---|---|---|---|
messages? | Message[] | Array of Chat SDK Message objects. Works with FetchResult.messages, thread.recentMessages, or any collected iterable. | |
options? | ToAiMessagesOptions | {} | Optional configuration. |
Options
| Prop | Type | Default | Description |
|---|---|---|---|
includeNames? | boolean | false | Prefix user messages with [username]: for multi-user context. |
transformMessage? | (aiMessage: AiMessage, source: Message) => AiMessage | null | Promise<AiMessage | null> | Transform or filter each message after default processing. Return null to skip the message. | |
onUnsupportedAttachment? | (attachment: Attachment, message: Message) => void | console.warn | Called when an attachment type is not supported (video, audio). |
Returns
Promise<AiMessage[]>: an array of messages with role and content fields, assignable to AI SDK's ModelMessage[].
Behavior
- Role mapping: messages with
author.isMe === truebecome"assistant", and all others become"user". - Filtering: a message with no text is kept when it has links or attachments the converter can include, meaning images and text files with a working
fetchData(). Messages with no text whose only attachments are unsupported (video, audio, other file types, or missingfetchData()) are removed. - Sorting: messages are sorted oldest first by
metadata.dateSent. - Links: link metadata (URL, title, description, site name) is appended to the message content. Third-party title, description, and site-name fields are normalized, length-limited, escaped, and enclosed in an explicit untrusted-content fence. Embedded message links are labeled
[Embedded message: ...]. - Attachments: images and text files (JSON, XML, YAML, and similar) are fetched with
fetchData()and included as multipart content. When the message has no text, thecontentarray contains only attachment parts, with no leading text part. Video and audio attachments triggeronUnsupportedAttachment.
Return types
User messages have multipart content when attachments are present:
Examples
Multi-user context
Prefix each user message with the sender's username so the model can tell speakers apart:
Transforming messages
Replace raw user IDs with readable names:
Filtering messages
Skip messages from a specific user:
Handling unsupported attachments
Supported attachment types
| Type | MIME types | Included as |
|---|---|---|
image | Any image MIME type | FilePart with base64 data |
file | text/*, application/json, application/xml, application/javascript, application/typescript, application/yaml, application/toml | FilePart with base64 data |
video | Any | Skipped (triggers onUnsupportedAttachment) |
audio | Any | Skipped (triggers onUnsupportedAttachment) |
file | Other (e.g. application/pdf) | Silently skipped |
Attachments require fetchData() to be available on the attachment object. Attachments without fetchData() are silently skipped.
Read more
Overview
AI utilities that ship with Chat SDK, including agent tools, message conversion, and supporting types.
AI SDK Tools
Let an AI agent post messages, send DMs, react, edit, and delete inside your workspace, with approval required for writes by default.
Types
TypeScript types exported from the chat/ai subpath.
Streaming
Stream real-time text responses from AI models and other async sources to chat platforms.