AI_UIMessageStreamError
This error occurs when a UI message stream contains invalid or out-of-sequence chunks.
Common causes:
- Receiving a
text-deltachunk without a precedingtext-startchunk - Receiving a
text-endchunk without a precedingtext-startchunk - Receiving a
reasoning-deltachunk without a precedingreasoning-startchunk - Receiving a
reasoning-endchunk without a precedingreasoning-startchunk - Receiving a
tool-input-deltachunk without a precedingtool-input-startchunk - Attempting to access a tool invocation that doesn't exist
This error often surfaces when an upstream request fails before any tokens are streamed and a custom transport tries to write an inline error message to the UI stream without the proper start chunk.
Properties
chunkType: The type of chunk that caused the error (e.g.,text-delta,reasoning-end,tool-input-delta)chunkId: The ID associated with the failing chunk (part ID or toolCallId)message: The error message with details about what went wrong
Checking for this Error
You can check if an error is an instance of AI_UIMessageStreamError using:
import { UIMessageStreamError } from 'ai';
if (UIMessageStreamError.isInstance(error)) { console.log('Chunk type:', error.chunkType); console.log('Chunk ID:', error.chunkId); // Handle the error}Common Solutions
-
Ensure proper chunk ordering: Always send a
*-startchunk before any*-deltaor*-endchunks for the same ID:// Correct orderwriter.write({ type: 'text-start', id: 'my-text' });writer.write({ type: 'text-delta', id: 'my-text', delta: 'Hello' });writer.write({ type: 'text-end', id: 'my-text' }); -
Verify IDs match: Ensure the
idused in*-deltaand*-endchunks matches theidused in the corresponding*-startchunk. -
Handle error paths correctly: When writing error messages in custom transports, ensure you emit the full start/delta/end sequence:
// When handling errors in custom transportswriter.write({ type: 'text-start', id: errorId });writer.write({type: 'text-delta',id: errorId,delta: 'Request failed...',});writer.write({ type: 'text-end', id: errorId }); -
Check stream producer logic: Review your streaming implementation to ensure chunks are sent in the correct order, especially when dealing with concurrent operations or merged streams.