[BUG] `@mastra/ai-sdk` drops `transient` from custom data chunks, so they are kept in `message.parts`
Describe the Bug
@mastra/ai-sdk drops the transient flag from custom data-* chunks, so transient parts end up in message.parts.
A tool streams live-preview frames:
await context.writer?.custom({ type: "data-preview", data: { step }, transient: true });convertFullStreamChunkToUIMessageStream rebuilds the chunk as { type, data, id }, so transient is gone before the AI SDK sees it. The AI SDK then appends every frame to message.parts, both in useChat and in the responseMessage from createUIMessageStream's onFinish.
DataChunkType documents transient as "streamed to the client but not persisted to storage", and the Mastra team confirmed on Slack that this is unintended.
The same pattern is at 7 sites on main:
- direct
writer.customchunks:helpers.tsL954 tool-outputchunks:helpers.tsL866- workflow streams:
transformers.tsL1294 and L1307 - network streams:
transformers.tsL1663, L1672 and L1774
Impact: one page generation streamed about 200 frames of about 60 KB each, which left a 6 MB assistant message. The next useChat request goes over Vercel's 4.5 MB body limit, so the conversation can't continue.
Steps To Reproduce
- Clone the gist, then run
npm installandnpm run repro. - The repro runs a mock-model agent whose tool writes three
transientdata-previewchunks, and passes the stream throughtoAISdkStream(stream, { from: "agent", version: "v6" })intocreateUIMessageStream({ onFinish }). - Output:
data-preview chunks on the UI stream: [{"type":"data-preview","data":{"step":1}}, …]
data-preview parts in responseMessage: 3Link to Minimal Reproducible Example
https://gist.github.com/feds01/a2bccfd508e9b0b31e2d02c0a073f9f5
Expected Behavior
The chunks keep transient: true, so onData still receives them but they never become message parts. With the fix below, the repro prints data-preview parts in responseMessage: 0.
Environment Information
System:
OS: macOS 26.7
CPU: (18) arm64 Apple M5 Pro
Binaries:
Node: 22.22.2
npm: 10.9.7
npmPackages:
@mastra/ai-sdk: 1.10.5
@mastra/core: 1.71.0
ai: 7.0.112
zod: 4.4.3
- LLM Provider: n/a in the repro (mock model); seen in production with Anthropic
- Browser: n/a (reproduced on the server)Proposed fix
A single toUIDataChunk helper in utils.ts that carries transient through, used at all 7 sites, plus tests in helpers.test.ts. We're happy to open the PR once this is approved.
diff -ru a/client-sdks/ai-sdk/src/helpers.test.ts b/client-sdks/ai-sdk/src/helpers.test.ts
--- a/client-sdks/ai-sdk/src/helpers.test.ts
+++ b/client-sdks/ai-sdk/src/helpers.test.ts
@@ -477,3 +477,30 @@
expect(uiChunk).not.toHaveProperty('providerMetadata');
});
});
+
+describe('custom data chunk transient flag', () => {
+ const transientChunk = { type: 'data-preview' as const, data: { step: 1 }, transient: true };
+
+ it('keeps transient on a chunk written with writer.custom', () => {
+ expect(convertFullStreamChunkToUIMessageStream({ part: transientChunk as any, onError: String })).toEqual(
+ transientChunk,
+ );
+ });
+
+ it('keeps transient on a chunk nested in tool-output', () => {
+ expect(
+ convertFullStreamChunkToUIMessageStream({
+ part: { type: 'tool-output', toolCallId: 'call-1', toolName: 'preview', output: transientChunk } as any,
+ onError: String,
+ }),
+ ).toEqual(transientChunk);
+ });
+
+ it('leaves transient absent when the chunk does not set it', () => {
+ const persistedChunk = { type: 'data-preview' as const, data: { step: 1 } };
+
+ expect(convertFullStreamChunkToUIMessageStream({ part: persistedChunk as any, onError: String })).toEqual(
+ persistedChunk,
+ );
+ });
+});
diff -ru a/client-sdks/ai-sdk/src/helpers.ts b/client-sdks/ai-sdk/src/helpers.ts
--- a/client-sdks/ai-sdk/src/helpers.ts
+++ b/client-sdks/ai-sdk/src/helpers.ts
@@ -17,7 +17,7 @@
} from '@internal/ai-v6';
import { DefaultGeneratedFile, DefaultGeneratedFileWithType } from '@mastra/core/stream';
import type { DataChunkType, ChunkType, MastraFinishReason } from '@mastra/core/stream';
-import { isDataChunkType } from './utils';
+import { isDataChunkType, toUIDataChunk } from './utils';
/**
* Separator used to encode both runId and toolCallId into a single approvalId string.
@@ -863,8 +863,7 @@
`UI Messages require a data property when using data- prefixed chunks \n ${JSON.stringify(part)}`,
);
}
- const { type, data, id } = output;
- return { type, data, ...(id !== undefined && { id }) } as InferUIMessageChunk<UI_MESSAGE>;
+ return toUIDataChunk(output) as InferUIMessageChunk<UI_MESSAGE>;
}
return;
}
@@ -951,8 +950,7 @@
`UI Messages require a data property when using data- prefixed chunks \n ${JSON.stringify(part)}`,
);
}
- const { type, data, id } = part;
- return { type, data, ...(id !== undefined && { id }) } as InferUIMessageChunk<UI_MESSAGE>;
+ return toUIDataChunk(part) as InferUIMessageChunk<UI_MESSAGE>;
}
return;
diff -ru a/client-sdks/ai-sdk/src/transformers.ts b/client-sdks/ai-sdk/src/transformers.ts
--- a/client-sdks/ai-sdk/src/transformers.ts
+++ b/client-sdks/ai-sdk/src/transformers.ts
@@ -26,6 +26,7 @@
isWorkflowExecutionDataChunkType,
safeParseErrorObject,
isMastraTextStreamChunk,
+ toUIDataChunk,
} from './utils';
type LanguageModelV2Usage = {
@@ -1291,8 +1292,7 @@
`UI Messages require a data property when using data- prefixed chunks \n ${JSON.stringify(output)}`,
);
}
- const { type, data, id } = output;
- return { type, data, ...(id !== undefined && { id }) };
+ return toUIDataChunk(output);
}
return null;
}
@@ -1304,13 +1304,7 @@
`UI Messages require a data property when using data- prefixed chunks \n ${JSON.stringify(payload)}`,
);
}
- const { type, data, id } = payload;
-
- return {
- type,
- data,
- ...(id !== undefined && { id }),
- };
+ return toUIDataChunk(payload);
}
return null;
}
@@ -1660,8 +1654,7 @@
);
}
- const { type, data, id } = payload.payload;
- return { type, data, ...(id !== undefined && { id }) };
+ return toUIDataChunk(payload.payload);
}
if (isWorkflowExecutionDataChunkType(payload)) {
if (!('data' in payload.payload)) {
@@ -1669,8 +1662,7 @@
`UI Messages require a data property when using data- prefixed chunks \n ${JSON.stringify(payload)}`,
);
}
- const { type, data, id } = payload.payload;
- return { type, data, ...(id !== undefined && { id }) };
+ return toUIDataChunk(payload.payload);
}
if (payload.type.startsWith('agent-execution-event-')) {
@@ -1771,8 +1763,7 @@
);
}
- const { type, data, id } = payload;
- return { type, data, ...(id !== undefined && { id }) };
+ return toUIDataChunk(payload);
}
return null;
}
diff -ru a/client-sdks/ai-sdk/src/utils.ts b/client-sdks/ai-sdk/src/utils.ts
--- a/client-sdks/ai-sdk/src/utils.ts
+++ b/client-sdks/ai-sdk/src/utils.ts
@@ -4,6 +4,17 @@
return chunk && typeof chunk === 'object' && 'type' in chunk && chunk.type?.startsWith('data-');
};
+/**
+ * Rebuilds a custom data chunk for the UI message stream. `transient` must survive: the AI SDK
+ * keeps a data part out of `message.parts` only when the chunk it receives says so.
+ */
+export const toUIDataChunk = ({ type, data, id, transient }: DataChunkType): DataChunkType => ({
+ type,
+ data,
+ ...(id !== undefined && { id }),
+ ...(transient !== undefined && { transient }),
+});
+
export const isMastraTextStreamChunk = (chunk: any): chunk is ChunkType<OutputSchema> => {
return (
chunk &&Verification
- I have searched the existing issues to make sure this is not a duplicate
- I have included sufficient information for the team to reproduce and understand the issue
Source: mastra-ai/mastra