fix(ai): add flush handler to smoothStream to prevent text loss#13219
Open
MaxwellCalkin wants to merge 2 commits intovercel:mainfrom
Open
fix(ai): add flush handler to smoothStream to prevent text loss#13219MaxwellCalkin wants to merge 2 commits intovercel:mainfrom
MaxwellCalkin wants to merge 2 commits intovercel:mainfrom
Conversation
…ream close The smoothStream transform was missing a flush handler on its TransformStream. When the stream closes (e.g., due to abort, error, or end of generation), any text remaining in the chunking buffer would be silently dropped. This is particularly noticeable with agent/tool-calling workflows where text segments can end abruptly before a complete word boundary is reached. The missing flush meant the last partial chunk of text before stream closure was lost. Add a flush() handler that calls flushBuffer() to emit any remaining buffered text when the writable side of the TransformStream closes. Fixes vercel#13199 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add tests verifying that the flush handler correctly emits remaining buffered text and reasoning when the stream closes without a trailing non-text chunk (e.g., text-end). Also adds a test for the text-before- tool-call-then-more-text pattern that occurs in agent workflows. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
Fixes #13199
The
smoothStreamtransform'sTransformStreamwas missing aflushhandler. This meant that when the stream closes (e.g., due to abort, error, or the end of generation), any text remaining in the chunking buffer was silently dropped rather than being emitted to the consumer.This is particularly noticeable in agent/tool-calling workflows where:
/\S+\s+/)The fix
Add a
flush()handler to theTransformStreamthat calls the existingflushBuffer()helper. This ensures any remaining buffered text or reasoning content is emitted when the writable side closes, matching the same behavior that already exists when non-smoothable chunks (liketool-call,text-end, etc.) arrive mid-stream.Changes
packages/ai/src/generate-text/smooth-stream.ts: Addflush(controller) { flushBuffer(controller); }to the TransformStream optionspackages/ai/src/generate-text/smooth-stream.test.ts: Add tests for:text-end)Disclosure
This PR was authored by an AI (Claude Opus 4.6, Anthropic). The GitHub account is human-owned. See maxwellcalkin.com for details.
🤖 Generated with Claude Code