0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00

fix(@mastra/core): always convert to ui messages before sending to LLM (#3230)

Fixes
https://discord.com/channels/1309558646228779139/1350882749094952980/1353072381786329181

We store LLM messages as core messages everywhere, but sending core
messages to the ai sdk can result in errors like this:

```
Error streaming from agent MessageConversionError [AI_MessageConversionError]: Unsupported role: 
tool
    at convertToCoreMessages (file:///Users/tylerbarnes/code/mastra-ai/mastra/node_modules/.pnpm/
ai@4.2.1_react@19.0.0_zod@3.24.2/node_modules/ai/dist/index.mjs:1866:15)
    at standardizePrompt (file:///Users/tylerbarnes/code/mastra-ai/mastra/node_modules/.pnpm/ai@4
.2.1_react@19.0.0_zod@3.24.2/node_modules/ai/dist/index.mjs:2101:53)
    at new DefaultStreamTextResult (file:///Users/tylerbarnes/code/mastra-ai/mastra/node_modules/
.pnpm/ai@4.2.1_react@19.0.0_zod@3.24.2/node_modules/ai/dist/index.mjs:5359:27)
    at streamText (file:///Users/tylerbarnes/code/mastra-ai/mastra/node_modules/.pnpm/ai@4.2.1_re
act@19.0.0_zod@3.24.2/node_modules/ai/dist/index.mjs:4989:10)
    at MastraLLM.__stream (file:///Users/tylerbarnes/code/mastra-ai/mastra/packages/core/dist/chu
nk-RQYZTVRX.js:425:18)
```

Which seems to imply that the AI SDK stream/generate utils expect UI
messages to be passed in, and they then convert those to core messages
internally.

So this PR:
1. Adds a test with useChat to catch the reported bug
2. Adds a convertToUIMessages method onto the llm class + uses that
method before passing any messages to AI SDK. It seems wasteful to
convert from core messages to UI messages only for AI SDK to convert
back to core messages, but I don't see another option. Related:
https://github.com/vercel/ai/issues/3161. As seen here
https://sdk.vercel.ai/docs/reference/ai-sdk-core/stream-text#messages it
says "Automatically converts UI messages from the useChat hook." so it's
possible this is an AI SKD bug and it's supposed to accept core messages
just fine - but it doesn't. The other possibility is in some cases we
have mixed CoreMessages and ui messages and that's triggering AI SDK to
call `convertToCoreMessages` which then leads to the error above
This commit is contained in:
Tyler Barnes 2025-03-24 16:51:25 -07:00 • committed by GitHub
commit b4fbc59f85
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 744 additions and 215 deletions