Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions .changeset/tender-poets-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@ai-sdk/ui-utils': patch
'@ai-sdk/react': patch
---

fix (ai/ui): forward streaming errors in useChat
21 changes: 20 additions & 1 deletion packages/react/src/use-chat.ui.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('stream data stream', () => {
);

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

it(
'should show error response when there is a streaming error',
withTestServer(
{
type: 'stream-values',
url: '/api/chat',
content: ['3:"custom error message"\n'],
},
async () => {
await userEvent.click(screen.getByTestId('do-append'));

await screen.findByTestId('error');
expect(screen.getByTestId('error')).toHaveTextContent(
'Error: custom error message',
);
},
),
);

describe('loading state', () => {
it(
'should show loading state',
Expand Down
4 changes: 4 additions & 0 deletions packages/ui-utils/src/parse-complex-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export async function parseComplexResponse({
for await (const { type, value } of readDataStream(reader, {
isAborted: () => abortControllerRef?.current === null,
})) {
if (type === 'error') {
throw new Error(value);
}

if (type === 'text') {
if (prefixMap['text']) {
prefixMap['text'] = {
Expand Down
Loading