AI SDK UIconvertToCoreMessages
convertToCoreMessages()
The convertToCoreMessages
function is used to transform an array of UI messages from the useChat
hook into an array of CoreMessage
objects. These CoreMessage
objects are compatible with AI core functions like streamText
.
app/api/chat/route.ts
import { openai } from '@ai-sdk/openai';import { convertToCoreMessages, streamText } from 'ai';
export async function POST(req: Request) { const { messages } = await req.json();
const result = await streamText({ model: openai('gpt-4o'), messages: convertToCoreMessages(messages), });
return result.toDataStreamResponse();}
Import
import { convertToCoreMessages } from "ai"
API Signature
Parameters
messages:
Message[]
An array of UI messages from the useChat hook to be converted
Returns
An array of CoreMessage
objects.
CoreMessage[]:
Array
An array of CoreMessage objects