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\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.
6 Replies
@Romain related to our other discussion
Hey @flippyhead ! The agent network is being reworked right now, it will be merged into the Agent primitive which is going to solve all these streaming issues. The only way right now would be for you to manually transform the stream using something like
stream.pipeThrough, not ideal, but it should work.Is that different than what the mastra playground is doing with its network chat stuff?
📝 Created GitHub issue: https://github.com/mastra-ai/mastra/issues/7477
GitHub
[DISCORD:1413131560764444703] Mastra vNext Network Stream Format In...
This issue was created from Discord post: https://discord.com/channels/1309558646228779139/1413131560764444703 Problem: When using Mastra's new vNext networks with the AI SDK's useChat hook...
With Agent Networks merged into an Agent, the output stream will be aisdk compatible. We are consolidating all primitives to stream in Mastra Format which can convert to AISDK.
We plan to start releasing the new stream format in anticipation of deprecating old things Sept 16
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View