Mastra vNext Network Stream Format Incompatibility with AI SDK

Problem:
When using Mastra's new vNext networks with the AI SDK's useChat hook, the stream
parsing fails with the error: Failed to parse stream string. Invalid code {"type".

Root Cause:
After adding detailed logging, I discovered that Mastra vNext networks return a
fundamentally different stream format than what the AI SDK expects:

What Mastra vNext Networks Return:
  • Content-Type: text/plain; charset=UTF-8
  • Format: Raw JSON objects concatenated together without delimiters
  • Example: {"type":"start","payload":{...}}{"type":"step-start","payload":{...}}{"type":"tool-call-delta","payload":{...}}What AI SDK's useChat Expects:
  • Content-Type: text/event-stream (for SSE) or properly formatted data stream
  • Format: Server-Sent Events (lines starting with data: followed by JSON) or
    newline-delimited JSON with specific prefixes
  • Example: data: {"type":"text-delta","text":"Hello"}\n\nThe Issue:The AI SDK's parseDataStreamPart function cannot parse the raw concatenated JSONobjects. It expects either:
  • SSE format with data: prefixes
  • Newline-delimited JSON with specific format codes (like 0:, 1:, 2: etc.)Current Setup:
  • Using @ai-sdk/react v1.1.21 with useChat hook
  • Mastra vNext network endpoint: /api/networks/v-next/{networkId}/stream
  • The messages ARE being created successfully on the backend (visible after page
    refresh)
  • This is purely a client-side stream parsing incompatibilityQuestion:Is there a recommended approach for integrating Mastra vNext networks with AI SDK?Should we:
  • Wait for Mastra to support AI SDK-compatible streaming format?
  • Create a custom API endpoint to transform the stream format?
  • Use a different approach altogether?The network functionality works great - the issue is just the stream format mismatchbetween what Mastra produces and what AI SDK consumes.
Was this page helpful?