Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
5659f3d
fix(ai): handle missing start chunks in processUIMessageStream
codewarnab Dec 30, 2025
3258205
Merge branch 'main' into fix/process-ui-stream-null-parts-clean
codewarnab Dec 30, 2025
83912fe
Merge branch 'main' into fix/process-ui-stream-null-parts-clean
codewarnab Jan 4, 2026
77255e6
Merge branch 'main' into fix/process-ui-stream-null-parts-clean
codewarnab Jan 5, 2026
2ba5d81
Merge branch 'main' into fix/process-ui-stream-null-parts-clean
codewarnab Jan 8, 2026
e034dfc
Merge branch 'main' into fix/process-ui-stream-null-parts-clean
aayush-kapoor Jan 8, 2026
9cbbe21
Merge branch 'main' into fix/process-ui-stream-null-parts-clean
codewarnab Jan 9, 2026
92d5dd6
Merge branch 'main' into fix/process-ui-stream-null-parts-clean
codewarnab Jan 9, 2026
6e2b1b9
Merge branch 'main' into fix/process-ui-stream-null-parts-clean
codewarnab Jan 9, 2026
047e713
fix(ui): add tests for missing text/reasoning start chunks
codewarnab Jan 9, 2026
b374c01
feat(ai): add UIMessageStreamError for malformed stream chunks
codewarnab Jan 13, 2026
67db24b
Merge branch 'main' into fix/process-ui-stream-null-parts-clean
codewarnab Jan 13, 2026
f491882
Merge branch 'main' into fix/process-ui-stream-null-parts-clean
aayush-kapoor Jan 13, 2026
cbee82a
Merge branch 'main' into fix/process-ui-stream-null-parts-clean
codewarnab Jan 13, 2026
a61eb8e
cs change
aayush-kapoor Jan 14, 2026
a6bdf9d
Merge branch 'main' into fix/process-ui-stream-null-parts-clean
aayush-kapoor Jan 14, 2026
1415615
pretty
aayush-kapoor Jan 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions packages/ai/src/ui/process-ui-message-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,105 @@ describe('processUIMessageStream', () => {
});
});

describe('malformed stream errors', () => {
it('should throw descriptive error when text-delta is received without text-start', async () => {
const stream = createUIMessageStream([
{ type: 'start', messageId: 'msg-123' },
{ type: 'start-step' },
{ type: 'text-delta', id: 'text-1', delta: 'Hello' },
]);

state = createStreamingUIMessageState({
messageId: 'msg-123',
lastMessage: undefined,
});

await expect(
consumeStream({
stream: processUIMessageStream({
stream,
runUpdateMessageJob,
onError: error => {
throw error;
},
}),
onError: error => {
throw error;
},
}),
).rejects.toThrow(
'Received text-delta for missing text part with ID "text-1". ' +
'Ensure a "text-start" chunk is sent before any "text-delta" chunks.',
);
});

it('should throw descriptive error when reasoning-delta is received without reasoning-start', async () => {
const stream = createUIMessageStream([
{ type: 'start', messageId: 'msg-123' },
{ type: 'start-step' },
{ type: 'reasoning-delta', id: 'reasoning-1', delta: 'Thinking...' },
]);

state = createStreamingUIMessageState({
messageId: 'msg-123',
lastMessage: undefined,
});

await expect(
consumeStream({
stream: processUIMessageStream({
stream,
runUpdateMessageJob,
onError: error => {
throw error;
},
}),
onError: error => {
throw error;
},
}),
).rejects.toThrow(
'Received reasoning-delta for missing reasoning part with ID "reasoning-1". ' +
'Ensure a "reasoning-start" chunk is sent before any "reasoning-delta" chunks.',
);
});

it('should throw descriptive error when tool-input-delta is received without tool-input-start', async () => {
const stream = createUIMessageStream([
{ type: 'start', messageId: 'msg-123' },
{ type: 'start-step' },
{
type: 'tool-input-delta',
toolCallId: 'tool-1',
inputTextDelta: '{"key":',
},
]);

state = createStreamingUIMessageState({
messageId: 'msg-123',
lastMessage: undefined,
});

await expect(
consumeStream({
stream: processUIMessageStream({
stream,
runUpdateMessageJob,
onError: error => {
throw error;
},
}),
onError: error => {
throw error;
},
}),
).rejects.toThrow(
'Received tool-input-delta for missing tool call with ID "tool-1". ' +
'Ensure a "tool-input-start" chunk is sent before any "tool-input-delta" chunks.',
);
});
});

describe('server-side tool roundtrip', () => {
beforeEach(async () => {
const stream = createUIMessageStream([
Expand Down
78 changes: 54 additions & 24 deletions packages/ai/src/ui/process-ui-message-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ export function createStreamingUIMessageState<UI_MESSAGE extends UIMessage>({
lastMessage?.role === 'assistant'
? lastMessage
: ({
id: messageId,
metadata: undefined,
role: 'assistant',
parts: [] as UIMessagePart<
InferUIMessageData<UI_MESSAGE>,
InferUIMessageTools<UI_MESSAGE>
>[],
} as UI_MESSAGE),
id: messageId,
metadata: undefined,
role: 'assistant',
parts: [] as UIMessagePart<
InferUIMessageData<UI_MESSAGE>,
InferUIMessageTools<UI_MESSAGE>
>[],
} as UI_MESSAGE),
activeTextParts: {},
activeReasoningParts: {},
partialToolCalls: {},
Expand Down Expand Up @@ -123,33 +123,33 @@ export function processUIMessageStream<UI_MESSAGE extends UIMessage>({
providerExecuted?: boolean;
title?: string;
} & (
| {
| {
state: 'input-streaming';
input: unknown;
providerExecuted?: boolean;
}
| {
| {
state: 'input-available';
input: unknown;
providerExecuted?: boolean;
providerMetadata?: ProviderMetadata;
}
| {
| {
state: 'output-available';
input: unknown;
output: unknown;
providerExecuted?: boolean;
preliminary?: boolean;
}
| {
| {
state: 'output-error';
input: unknown;
rawInput?: unknown;
errorText: string;
providerExecuted?: boolean;
providerMetadata?: ProviderMetadata;
}
),
),
) {
const part = state.message.parts.find(
part =>
Expand Down Expand Up @@ -206,28 +206,28 @@ export function processUIMessageStream<UI_MESSAGE extends UIMessage>({
providerExecuted?: boolean;
title?: string;
} & (
| {
| {
state: 'input-streaming';
input: unknown;
}
| {
| {
state: 'input-available';
input: unknown;
providerMetadata?: ProviderMetadata;
}
| {
| {
state: 'output-available';
input: unknown;
output: unknown;
preliminary: boolean | undefined;
}
| {
| {
state: 'output-error';
input: unknown;
errorText: string;
providerMetadata?: ProviderMetadata;
}
),
),
) {
const part = state.message.parts.find(
part =>
Expand Down Expand Up @@ -313,6 +313,12 @@ export function processUIMessageStream<UI_MESSAGE extends UIMessage>({

case 'text-delta': {
const textPart = state.activeTextParts[chunk.id];
if (textPart == null) {
throw new Error(
`Received text-delta for missing text part with ID "${chunk.id}". ` +
`Ensure a "text-start" chunk is sent before any "text-delta" chunks.`,
);
}
textPart.text += chunk.delta;
textPart.providerMetadata =
chunk.providerMetadata ?? textPart.providerMetadata;
Expand All @@ -322,6 +328,12 @@ export function processUIMessageStream<UI_MESSAGE extends UIMessage>({

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.

`Received text-end for missing text part with ID "${chunk.id}". ` +
`Ensure a "text-start" chunk is sent before any "text-end" chunks.`,
);
}
textPart.state = 'done';
textPart.providerMetadata =
chunk.providerMetadata ?? textPart.providerMetadata;
Expand All @@ -345,6 +357,12 @@ export function processUIMessageStream<UI_MESSAGE extends UIMessage>({

case 'reasoning-delta': {
const reasoningPart = state.activeReasoningParts[chunk.id];
if (reasoningPart == null) {
throw new Error(
`Received reasoning-delta for missing reasoning part with ID "${chunk.id}". ` +
`Ensure a "reasoning-start" chunk is sent before any "reasoning-delta" chunks.`,
);
}
reasoningPart.text += chunk.delta;
reasoningPart.providerMetadata =
chunk.providerMetadata ?? reasoningPart.providerMetadata;
Expand All @@ -354,6 +372,12 @@ export function processUIMessageStream<UI_MESSAGE extends UIMessage>({

case 'reasoning-end': {
const reasoningPart = state.activeReasoningParts[chunk.id];
if (reasoningPart == null) {
throw new Error(
`Received reasoning-end for missing reasoning part with ID "${chunk.id}". ` +
`Ensure a "reasoning-start" chunk is sent before any "reasoning-end" chunks.`,
);
}
reasoningPart.providerMetadata =
chunk.providerMetadata ?? reasoningPart.providerMetadata;
reasoningPart.state = 'done';
Expand Down Expand Up @@ -440,6 +464,12 @@ export function processUIMessageStream<UI_MESSAGE extends UIMessage>({

case 'tool-input-delta': {
const partialToolCall = state.partialToolCalls[chunk.toolCallId];
if (partialToolCall == null) {
throw new Error(
`Received tool-input-delta for missing tool call with ID "${chunk.toolCallId}". ` +
`Ensure a "tool-input-start" chunk is sent before any "tool-input-delta" chunks.`,
);
}

partialToolCall.text += chunk.inputTextDelta;

Expand Down Expand Up @@ -684,12 +714,12 @@ export function processUIMessageStream<UI_MESSAGE extends UIMessage>({
const existingUIPart =
dataChunk.id != null
? (state.message.parts.find(
chunkArg =>
dataChunk.type === chunkArg.type &&
dataChunk.id === chunkArg.id,
) as
| DataUIPart<InferUIMessageData<UI_MESSAGE>>
| undefined)
chunkArg =>
dataChunk.type === chunkArg.type &&
dataChunk.id === chunkArg.id,
) as
| DataUIPart<InferUIMessageData<UI_MESSAGE>>
| undefined)
: undefined;

if (existingUIPart != null) {
Expand Down