Skip to content

Conversation

@codewarnab
Copy link
Contributor

@codewarnab codewarnab commented Dec 30, 2025

This PR addresses a runtime error: TypeError: Cannot read properties of undefined (reading 'text') in processUIMessageStream.

Issue

The issue occurs when text-delta, reasoning-delta, or tool-input-delta chunks 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, processUIMessageStream assumed that a start part (initialized in activeTextParts, etc.) always existed when a delta arrived. When it didn't, accessing properties like textPart.text caused 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:

"Received text-delta for missing text part with ID "text-1". Ensure a "text-start" chunk is sent before any "text-delta" chunks."

This descriptive error aids in debugging the actual upstream issue (broken stream) rather than crashing the UI with an opaque TypeError.

Changes

  • packages/ai/src/ui/process-ui-message-stream.ts: Added guarding if (part == null) checks in text-delta, reasoning-delta, and tool-input-delta cases.
  • packages/ai/src/ui/process-ui-message-stream.test.ts:
    • Added test cases simulating malformed streams (delta without start) for text, reasoning, and tool-inputs.
    • Verified that specific descriptive errors are thrown.
    • Updated test harness consumeStream to properly propagate errors for validation.

Related Issues

Fixes #11700

Copilot AI review requested due to automatic review settings January 4, 2026 19:02
Copy link
Contributor

Copilot AI left a 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.

@aayush-kapoor
Copy link
Contributor

can you first address all the comments of the above bots?

@aayush-kapoor aayush-kapoor self-assigned this Jan 8, 2026
@codewarnab
Copy link
Contributor Author

can you first address all the comments of the above bots?

addressed all of them

@aayush-kapoor
Copy link
Contributor

since you already have the PR, can you create an issue and in the issue have a brief repro of where you encountered this error please. tag me so that i can take a look at it

case 'text-end': {
const textPart = state.activeTextParts[chunk.id];
if (textPart == null) {
throw new Error(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be good to have a dedicated error AI SDK error class for this, e.g. UIMessageStreamError

Copy link
Contributor Author

@codewarnab codewarnab Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a dedicated UIMessageStreamError class following the AI SDK error patterns.

@lgrammel
Copy link
Collaborator

please add a changeset

@lgrammel
Copy link
Collaborator

does this break backwards compat, i.e. are there cases where it might have worked before but would error after this change?

@codewarnab
Copy link
Contributor Author

please add a changeset

added

@codewarnab
Copy link
Contributor Author

codewarnab commented Jan 13, 2026

does this break backwards compat, i.e. are there cases where it might have worked before but would error after this change?
Backwards Compatibility Analysis:

This change should not break backwards compatibility for valid stream usage.

Scenario Before After
Valid stream (proper *-start*-delta*-end ordering) ✅ Works ✅ Works (no change)
Malformed stream (missing *-start chunks) TypeError: Cannot read properties of undefined ✅ Descriptive UIMessageStreamError

@codewarnab codewarnab force-pushed the fix/process-ui-stream-null-parts-clean branch from 148437e to b374c01 Compare January 13, 2026 13:14
@aayush-kapoor
Copy link
Contributor

hold off on merging this - there might be a different problem than the original for which the PR was raised

Previously, receiving certain malformed chunks would crash with an unhelpful TypeError. Now throws a dedicated UIMessageStreamError with clear, actionable error messages that include the chunk type and ID for easier debugging.
@aayush-kapoor aayush-kapoor changed the title fix(ai): handle missing start chunks in processUIMessageStream fix(ai): throw error for missing start chunks in processUIMessageStream Jan 14, 2026
Copy link
Contributor

@aayush-kapoor aayush-kapoor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run pnpm prettier-fix to fix failing CI

@aayush-kapoor aayush-kapoor merged commit d7e7f1f into vercel:main Jan 14, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useChat UIMessageStream throws TypeError when text-delta/text-end emitted without text-start (unhelpful error, masks original failure)

3 participants