Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/ai/src/generate-text/stream-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ function createOutputTransformStream<
let textChunk = '';
let textProviderMetadata: ProviderMetadata | undefined = undefined;
let lastPublishedJson = '';
const isTextOutput = output.name === 'text';

function publishTextChunk({
controller,
Expand Down Expand Up @@ -696,6 +697,17 @@ function createOutputTransformStream<
textChunk += chunk.text;
textProviderMetadata = chunk.providerMetadata ?? textProviderMetadata;

// For text output, every text-delta always changes the partial output,
// so we can publish immediately and avoid the expensive JSON.stringify
// comparison that causes O(n²) memory growth with large responses.
if (isTextOutput) {
publishTextChunk({
controller,
partialOutput: text as InferPartialOutput<OUTPUT>,
});
return;
}

// only publish if partial json can be parsed:
const result = await output.parsePartialOutput({ text });

Expand Down
Loading