You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the `Page` component, the `useChat` hook will request to your AI provider endpoint whenever the user submits a message. The messages are then streamed back in real-time and displayed in the chat UI.
68
+
In the `Page` component, the `useChat` hook will request to your AI provider endpoint whenever the user submits a message.
69
+
The messages are then streamed back in real-time and displayed in the chat UI.
70
+
71
+
This enables a seamless chat experience where the user can see the AI response as soon as it is available,
72
+
without having to wait for the entire response to be received.
73
73
74
-
This enables a seamless chat experience where the user can see the AI response as soon as it is available, without having to wait for the entire response to be received.
74
+
<Note>
75
+
`useChat` has a `keepLastMessageOnError` option that defaults to `false`. This
76
+
option can be enabled to keep the last message on error. We will make this the
77
+
default behavior in the next major release. Please enable it and update your
78
+
error handling/resubmit behavior.
79
+
</Note>
75
80
76
81
## Customized UI
77
82
78
83
`useChat` also provides ways to manage the chat message and input states via code, show loading and error states, and update messages without being triggered by user interactions.
79
84
80
-
### Loading and error states
85
+
### Loading State
81
86
82
-
To show a loading spinner while the chatbot is processing the user's message, you can use the `isLoading` state returned by the `useChat` hook:
87
+
The `isLoading` state returned by the `useChat` hook can be used for several
88
+
purposes
83
89
84
-
```tsx
85
-
const { isLoading, ... } =useChat()
90
+
- To show a loading spinner while the chatbot is processing the user's message.
91
+
- To show a "Stop" button to abort the current message.
Similarly, the `error` state reflects the error object thrown during the fetch request. It can be used to display an error message, or show a toast notification:
137
+
### Error State
93
138
94
-
```tsx
95
-
const {error, ...} = useChat()
139
+
Similarly, the `error` state reflects the error object thrown during the fetch request.
140
+
It can be used to display an error message, disable the submit button, or show a retry button.
96
141
97
-
useEffect(() => {
98
-
if (error) {
99
-
toast.error(error.message)
100
-
}
101
-
}, [error])
142
+
<Note>
143
+
We recommend showing a generic error message to the user, such as "Something
144
+
went wrong." This is a good practice to avoid leaking information from the
Copy file name to clipboardexpand all lines: content/docs/05-ai-sdk-ui/21-error-handling.mdx
+77-13
Original file line number
Diff line number
Diff line change
@@ -5,27 +5,91 @@ description: Learn how to handle errors in the AI SDK UI
5
5
6
6
# Error Handling
7
7
8
-
Errors can be handled by passing an [`onError`](/docs/reference/ai-sdk-ui/use-chat#on-error) callback function as an option to the [`useChat`](/docs/reference/ai-sdk-ui/use-chat), [`useCompletion`](/docs/reference/ai-sdk-ui/use-completion) or [`useAssistant`](/docs/reference/ai-sdk-ui/use-assistant) hooks.
8
+
### Error Handling Callback
9
9
10
-
Each AI SDK UI hook also returns an [error](/docs/reference/ai-sdk-ui/use-chat#error) object that you can use to render the error in your UI.
10
+
Errors can be processed by passing an [`onError`](/docs/reference/ai-sdk-ui/use-chat#on-error) callback function as an option to the [`useChat`](/docs/reference/ai-sdk-ui/use-chat), [`useCompletion`](/docs/reference/ai-sdk-ui/use-completion) or [`useAssistant`](/docs/reference/ai-sdk-ui/use-assistant) hooks.
11
+
The callback function receives an error object as an argument.
11
12
12
-
```tsx
13
+
```tsx file="app/page.tsx" highlight="7-10"
13
14
import { useChat } from'ai/react';
14
15
15
-
const { ... } =useChat({
16
-
onError: error=> {
17
-
// handle error
18
-
console.error(error);
19
-
},
20
-
});
16
+
exportdefaultfunction Page() {
17
+
const {
18
+
/* ... */
19
+
} =useChat({
20
+
onError: error=> {
21
+
// handle error
22
+
console.error(error);
23
+
},
24
+
});
25
+
}
21
26
```
22
27
28
+
### Error Helper Object
29
+
23
30
Each AI SDK UI hook also returns an [error](/docs/reference/ai-sdk-ui/use-chat#error) object that you can use to render the error in your UI.
31
+
You can use the error object to show an error message, disable the submit button, or show a retry button.
32
+
33
+
<Note>
34
+
We recommend showing a generic error message to the user, such as "Something
35
+
went wrong." This is a good practice to avoid leaking information from the
Copy file name to clipboardexpand all lines: content/docs/07-reference/ai-sdk-ui/01-use-chat.mdx
+6
Original file line number
Diff line number
Diff line change
@@ -43,6 +43,12 @@ Allows you to easily create a conversational user interface for your chatbot app
43
43
type: "string = '/api/chat'",
44
44
description: 'The chat completion API endpoint offered by the provider.',
45
45
},
46
+
{
47
+
name: 'keepLastMessageOnError',
48
+
type: 'boolean',
49
+
description:
50
+
'Keeps the last message when an error happens. This will be the default behavior starting with the next major release. The flag was introduced for backwards compatibility and currently defaults to `false`. Please enable it and update your error handling/resubmit behavior.',
0 commit comments