Commit 4a86d3b
fix(ai): skip stringifying text when streaming partial text (#14123)
## Background
#13839
streamText with the default text output called JSON.stringify on the
full accumulated text on every single streaming chunk that creates
increasingly large string copies per stream casuing memory issues
## Summary
- skip `JSON.stringify` when the partial output is already a string
- structured outputs still go through stringify as before since they
need serialization to compare.
- compare the text directly
- no extra full-string serialization per chunk
## Manual Verification
tried reproducing via
<details>
<summary>repro</summary>
```ts
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
import { run } from '../../lib/run';
run(async () => {
const result = streamText({
model: openai.responses('gpt-4o-mini'),
prompt:
'Write an extremely detailed 5000-word essay about the history of computing. Include every detail you can.',
});
let chunks = 0;
for await (const textPart of result.textStream) {
chunks++;
if (chunks % 100 === 0) {
const mb = (process.memoryUsage().heapUsed / 1024 / 1024).toFixed(1);
console.log(`chunk ${chunks} — heap: ${mb}MB`);
}
}
console.log(`\nTotal chunks: ${chunks}`);
console.log(
`Final heap: ${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(1)}MB`,
);
});
```
</details>
## Checklist
- [x] Tests have been added / updated (for bug fixes / features)
- [ ] Documentation has been added / updated (for bug fixes / features)
- [x] A _patch_ changeset for relevant packages has been added (for bug
fixes / features - run `pnpm changeset` in the project root)
- [x] I have reviewed this pull request (self-review)
## Related Issues
fixes #138391 parent 9de7d7b commit 4a86d3b
3 files changed
Lines changed: 58 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15190 | 15190 | | |
15191 | 15191 | | |
15192 | 15192 | | |
| 15193 | + | |
| 15194 | + | |
| 15195 | + | |
| 15196 | + | |
| 15197 | + | |
| 15198 | + | |
| 15199 | + | |
| 15200 | + | |
| 15201 | + | |
| 15202 | + | |
| 15203 | + | |
| 15204 | + | |
| 15205 | + | |
| 15206 | + | |
| 15207 | + | |
| 15208 | + | |
| 15209 | + | |
| 15210 | + | |
| 15211 | + | |
| 15212 | + | |
| 15213 | + | |
| 15214 | + | |
| 15215 | + | |
| 15216 | + | |
| 15217 | + | |
| 15218 | + | |
| 15219 | + | |
| 15220 | + | |
| 15221 | + | |
| 15222 | + | |
| 15223 | + | |
| 15224 | + | |
| 15225 | + | |
| 15226 | + | |
| 15227 | + | |
| 15228 | + | |
| 15229 | + | |
| 15230 | + | |
| 15231 | + | |
| 15232 | + | |
| 15233 | + | |
| 15234 | + | |
| 15235 | + | |
| 15236 | + | |
15193 | 15237 | | |
15194 | 15238 | | |
15195 | 15239 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
600 | 600 | | |
601 | 601 | | |
602 | 602 | | |
603 | | - | |
| 603 | + | |
604 | 604 | | |
605 | 605 | | |
606 | 606 | | |
| |||
673 | 673 | | |
674 | 674 | | |
675 | 675 | | |
676 | | - | |
677 | | - | |
678 | | - | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
679 | 683 | | |
680 | | - | |
| 684 | + | |
681 | 685 | | |
682 | 686 | | |
683 | 687 | | |
| |||
0 commit comments