-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(ai): handle missing start chunks in processUIMessageStream #11463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(ai): handle missing start chunks in processUIMessageStream #11463
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds defensive null checks to prevent runtime errors when processing malformed UI message streams. The fix addresses cases where delta or end chunks are received before their corresponding start chunks.
Summary
The PR adds null checks for text-delta, text-end, reasoning-delta, reasoning-end, and tool-input-delta chunk types to prevent crashes when start chunks are missing. Instead of crashing with a generic TypeError, the code now throws descriptive errors that help identify the upstream issue.
Key Changes
- Added null checks with descriptive error messages for delta and end chunks in stream processing
- Added test cases to verify error handling for malformed streams (delta without start)
- Minor formatting improvements to type definitions
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| packages/ai/src/ui/process-ui-message-stream.ts | Added null checks for text, reasoning, and tool-input parts with descriptive error messages; minor formatting improvements |
| packages/ai/src/ui/process-ui-message-stream.test.ts | Added test cases for malformed stream scenarios where delta chunks are received without corresponding start chunks |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
can you first address all the comments of the above bots? |
addressed all of them |
This PR addresses a runtime error:
TypeError: Cannot read properties of undefined (reading 'text')inprocessUIMessageStream.Issue
The issue occurs when
text-delta,reasoning-delta, ortool-input-deltachunks are received before their corresponding start chunks (e.g.,text-start). This can happen in malformed streams, network issues causing packet reordering, or specific backend behaviors where a delta is emitted immediately without a start frame.Previously,
processUIMessageStreamassumed that a start part (initialized inactiveTextParts, etc.) always existed when a delta arrived. When it didn't, accessing properties liketextPart.textcaused a crash.Fix
This change adds explicit null checks for the active parts (
textPart,reasoningPart,partialToolCall). If a delta is received for a missing part, it now throws a descriptive error:This descriptive error aids in debugging the actual upstream issue (broken stream) rather than crashing the UI with an opaque TypeError.
Changes
if (part == null)checks intext-delta,reasoning-delta, andtool-input-deltacases.consumeStreamto properly propagate errors for validation.