feat: add experimental_streamMode 'single-consumer' to streamText#17501
Open
Aayush-engineer wants to merge 1 commit into
Open
feat: add experimental_streamMode 'single-consumer' to streamText#17501Aayush-engineer wants to merge 1 commit into
Aayush-engineer wants to merge 1 commit into
Conversation
Aayush-engineer
force-pushed
the
feat/single-consumer-stream-mode
branch
2 times, most recently
from
July 18, 2026 11:52
5ce1ce9 to
f9478cd
Compare
Adds an opt-in experimental_streamMode: 'single-consumer' option to streamText(). When set, the internal stream is not split via ReadableStream.tee() in teeStream(), avoiding the server-side buffering tee() introduces for large/long-running streams (see vercel#16753). Only one of textStream, stream/fullStream, consumeStream(), partialOutputStream, elementStream, or a final-result getter (usage, steps, finishReason, etc.) may be accessed in this mode; accessing a second one throws, since there is no buffered copy left to read. Fixes vercel#16753
Aayush-engineer
force-pushed
the
feat/single-consumer-stream-mode
branch
from
July 18, 2026 12:33
f9478cd to
0793f55
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
streamText().textStream(and every other stream/result accessor) routes throughteeStream(), which callsReadableStream.tee()to allow multiple independent readers. The source comment onteeStream()already notes this buffers stream content on the server and assumes LLM output is small enough not to matter.That assumption breaks down for long-running/large-output streams. #16753 reports high memory p99 and memory-limit breaches in a 128 MB Cloudflare Worker when consuming
textStreamfor long generations, caused by this buffering. The reporter had to bypassstreamTextinternals entirely and readdoStream()directly to avoid it.Summary
Adds an opt-in
experimental_streamMode: 'single-consumer'option tostreamText(). When set,teeStream()skipsReadableStream.tee()entirely and hands the internalbaseStreamdirectly to the first caller. Any second attempt to read the stream — viatextStream,stream/fullStream,consumeStream(),partialOutputStream,elementStream, or a final-result getter (usage,steps,finishReason, etc.) — throws anAISDKError, since there's no buffered copy left for a second reader.Reading a final-result getter after another consumer has already fully drained the stream still works, since the result promises are already resolved by that point — the restriction only applies to reading the underlying stream a second time.
Contributor Credit
Thanks to @matthew-gizmo for the original report and reproduction in #16753.
End-to-End Verification
stream-text.test.tssuite (408 existing tests) — all pass unmodified.textStreamconsumption in single-consumer mode, reading a single final-result getter, throwing on a second consumer access before the first drains, and throwing synchronously on a direct double-access ofstream.tsc --buildandultracite check(repo lint/format) both pass clean.Future Work
partialOutputStreamandelementStreamgo through the sameteeStream()path and should behave the same way in single-consumer mode, but aren't covered by dedicated tests yet.Related Issues
Fixes #16753