Surprising no one, I'm working on a chat UI for Minima.
Having already dealt with several major version upgrades across my dependencies during the last nine months of development, Vercel's AI SDK v5 is now in beta.
I've been investigating this issue since the v5 alpha, and while I haven't cracked the underlying cause, I'm happy with the solution, so I figured I'd share it while the v5 docs are catching up.
Once the messages array I was sending from the client to the chat endpoint contained a tool call result, the endpoint would start throwing 500 errors before the event handler began executing. I originally figured it must have been something to do with the size of the post body, but that investigation went nowhere. It's something deep in H3/Nitro internals that I haven't been able to get to the bottom of.
I eventually found the Sending only the last message section in the docs around message persistence, but there's two issues here – it's React only, and it's for v4.
Diving into the v5 internals, I found prepareSendMessagesRequest
in the chat transport class.
You can configure the chat transport to send only the last message to your endpoint like so:
const chat = new Chat({
transport: new DefaultChatTransport({
api,
prepareSendMessagesRequest({ messages }) {
return {
body: {
messages: [messages[messages.length - 1]],
},
}
},
}),
})
Then, in your endpoint, retrieve the existing messages for the chat from your database before you convertToModelMessages
.
Hope this helps.