MastraM
Mastra5d ago
ldp

How to migrate off of `format:'aisdk'` with custom Next route?

We have a next.js app with an embedded mastra instance.

We have a custom /api/chat that performs a bunch of business logic before streaming the response.

// /api/chat
export async function POST(req: Request) {
  // lots of business logic...

  const stream = await myAgent.stream(messages, {
    format: "aisdk", // Enable AI SDK v5 compatibility


I'm following the upgrade guide here

it says:
The format: "aisdk" option in stream()/generate() methods is deprecated. Use the @mastra/ai-sdk package instead. Learn more in the Using Vercel AI SDK documentation.

When I follow that link it says to use chatRoute() which "automatically formats the agent stream into an AI SDK-compatible format".

example given:
export const mastra = new Mastra({
  server: {
    apiRoutes: [
      chatRoute({
        path: "/chat",
        agent: "weatherAgent",
      }),
    ],
  },
});


This looks like nice DX if you're deploying a standalone mastra server, but does this even work if I'm embedding mastra into my next app?

Is there an equivalent way (as before) that I can use aisdk in my own api route?
Was this page helpful?