fix: lazily create text/reasoning parts when resumed stream starts with delta#13228
Open
MaxwellCalkin wants to merge 1 commit intovercel:mainfrom
Open
fix: lazily create text/reasoning parts when resumed stream starts with delta#13228MaxwellCalkin wants to merge 1 commit intovercel:mainfrom
MaxwellCalkin wants to merge 1 commit intovercel:mainfrom
Conversation
…l#13160) When useChat resumeStream resumes after a disconnect, the resumed stream may begin with text-delta or reasoning-delta chunks whose corresponding text-start/reasoning-start was already sent before the disconnect. Previously this threw a UIMessageStreamError, crashing the resumed stream. Instead of throwing, lazily create the missing text or reasoning part (as if a start chunk had been received) and continue processing the delta normally. This applies to text-delta, text-end, reasoning-delta, and reasoning-end chunks. Fixes vercel#13160 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 #13160
When
useChatresumeStreamresumes a UI stream after a disconnect, the resumed stream may start withtext-delta(orreasoning-delta) chunks for text/reasoning parts whosetext-start/reasoning-startwas already sent before the disconnect. The SDK currently throws aUIMessageStreamErrorbecausestate.activeTextParts[chunk.id]isundefined.This PR changes the
text-delta,text-end,reasoning-delta, andreasoning-endhandlers inprocessUIMessageStreamto lazily create the missing part instead of throwing. When a delta/end chunk arrives for an unknown part ID, a new empty part is initialized in the active parts map and pushed tostate.message.parts, then processing continues normally. This makes stream resumption work seamlessly.Changes
text-delta: Ifstate.activeTextParts[chunk.id]is null, create a newTextUIPartwith empty text in streaming state, register it, then append the deltatext-end: Same lazy creation, then immediately mark as donereasoning-delta: Ifstate.activeReasoningParts[chunk.id]is null, create a newReasoningUIPartwith empty text in streaming state, register it, then append the deltareasoning-end: Same lazy creation, then immediately mark as doneTests updated
The existing tests that expected
UIMessageStreamErrorthrows for these cases have been updated to verify the lazy creation behavior instead, with proper delta accumulation and end-state assertions.Test plan
process-ui-message-stream.test.tspassuseChatresumeStreamworks when resumed stream starts withtext-deltafor an existing part🤖 Generated with Claude Code