Skip to content

Commit e5b58f3

Browse files
authored
fix (ai/ui): forward streaming errors in useChat (#2618)
1 parent 9ada023 commit e5b58f3

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

.changeset/tender-poets-itch.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@ai-sdk/ui-utils': patch
3+
'@ai-sdk/react': patch
4+
---
5+
6+
fix (ai/ui): forward streaming errors in useChat

packages/react/src/use-chat.ui.test.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe('stream data stream', () => {
121121
);
122122

123123
it(
124-
'should show error response',
124+
'should show error response when there is a server error',
125125
withTestServer(
126126
{ type: 'error', url: '/api/chat', status: 404, content: 'Not found' },
127127
async () => {
@@ -135,6 +135,25 @@ describe('stream data stream', () => {
135135
),
136136
);
137137

138+
it(
139+
'should show error response when there is a streaming error',
140+
withTestServer(
141+
{
142+
type: 'stream-values',
143+
url: '/api/chat',
144+
content: ['3:"custom error message"\n'],
145+
},
146+
async () => {
147+
await userEvent.click(screen.getByTestId('do-append'));
148+
149+
await screen.findByTestId('error');
150+
expect(screen.getByTestId('error')).toHaveTextContent(
151+
'Error: custom error message',
152+
);
153+
},
154+
),
155+
);
156+
138157
describe('loading state', () => {
139158
it(
140159
'should show loading state',

packages/ui-utils/src/parse-complex-response.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ export async function parseComplexResponse({
8989
for await (const { type, value } of readDataStream(reader, {
9090
isAborted: () => abortControllerRef?.current === null,
9191
})) {
92+
if (type === 'error') {
93+
throw new Error(value);
94+
}
95+
9296
if (type === 'text') {
9397
if (prefixMap['text']) {
9498
prefixMap['text'] = {

0 commit comments

Comments
 (0)