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\n
The Issue:
The AI SDK's parseDataStreamPart function cannot parse the raw concatenated JSON
objects. It expects either:
1. SSE format with data: prefixes
2. 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 incompatibility
Question:
Is there a recommended approach for integrating Mastra vNext networks with AI SDK?
Should we:
1. Wait for Mastra to support AI SDK-compatible streaming format?
2. Create a custom API endpoint to transform the stream format?
3. Use a different approach altogether?
The network functionality works great - the issue is just the stream format mismatch
between what Mastra produces and what AI SDK consumes.