convertToModelMessages()
The convertToModelMessages
function is no longer required. The AI SDK now
automatically converts the incoming messages to the ModelMessage
format.
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 { openai } from '@ai-sdk/openai';import { convertToModelMessages, streamText } from 'ai';
export async function POST(req: Request) { const { messages } = await req.json();
const result = streamText({ model: openai('gpt-4o'), messages: convertToModelMessages(messages), });
return result.toUIMessageStreamResponse();}
Import
import { convertToModelMessages } from "ai"
API Signature
Parameters
messages:
options:
Returns
An array of ModelMessage
objects.
ModelMessage[]:
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.
import { tool } from 'ai';import { z } from 'zod';
const screenshotTool = tool({ parameters: z.object({}), execute: async () => 'imgbase64', experimental_toToolResultContent: result => [{ type: 'image', data: result }],});
const result = streamText({ model: openai('gpt-4'), messages: convertToModelMessages(messages, { tools: { screenshot: screenshotTool, }, }),});
Tools can implement the optional experimental_toToolResultContent
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.