ai
Version:
AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.
165 lines (131 loc) • 4.3 kB
text/mdx
---
title: Error Handling
description: Learn how to handle errors in the AI SDK Core
---
Regular errors are thrown and can be handled using the `try/catch` block.
```ts highlight="4,9-11"
import { generateText } from 'ai';
__PROVIDER_IMPORT__;
try {
const { text } = await generateText({
model: __MODEL__,
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
} catch (error) {
// handle error
}
```
See [Error Types](/docs/reference/ai-sdk-errors) for more information on the different types of errors that may be thrown.
When errors occur during streams that do not support error chunks,
the error is thrown as a regular error.
You can handle these errors using the `try/catch` block.
```ts highlight="4,13-15"
import { streamText } from 'ai';
__PROVIDER_IMPORT__;
try {
const { textStream } = streamText({
model: __MODEL__,
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
for await (const textPart of textStream) {
process.stdout.write(textPart);
}
} catch (error) {
// handle error
}
```
The `stream` result supports error parts.
You can handle those parts similar to other parts.
It is recommended to also add a try-catch block for errors that
happen outside of the streaming.
```ts highlight="14-22"
import { StreamProviderError, streamText } from 'ai';
__PROVIDER_IMPORT__;
try {
const { stream } = streamText({
model: __MODEL__,
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
for await (const part of stream) {
switch (part.type) {
// ... handle other part types
case 'error': {
const error = part.error;
if (StreamProviderError.isInstance(error)) {
console.error(error.message, {
type: error.type,
code: error.code,
statusCode: error.statusCode,
isRetryable: error.isRetryable,
});
}
break;
}
case 'abort': {
// handle stream abort
break;
}
case 'tool-error': {
const error = part.error;
// handle error
break;
}
}
}
} catch (error) {
// handle error
}
```
Well-formed provider error events that arrive after streaming starts are
normalized into [`StreamProviderError`](/docs/reference/ai-sdk-errors/ai-stream-provider-error)
instances. The same instance is supplied to callbacks such as `onError` and to
the `error` part in the full stream. The SDK does not automatically restart a
partially consumed stream; use `isRetryable` to implement application-managed
retries and decide how to handle any partial output.
When streams are aborted (e.g., via chat stop button), you may want to perform cleanup operations like updating stored messages in your UI. Use the `onAbort` callback to handle these cases.
The `onAbort` callback is called when a stream is aborted via `AbortSignal`, but `onEnd` is not called. This ensures you can still update your UI state appropriately.
```ts highlight="6-10"
import { streamText } from 'ai';
__PROVIDER_IMPORT__;
const { textStream } = streamText({
model: __MODEL__,
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
onAbort: ({ steps }) => {
// Update stored messages or perform cleanup
console.log('Stream aborted after', steps.length, 'steps');
},
onEnd: ({ steps, totalUsage }) => {
// This is called on normal completion
console.log('Stream completed normally');
},
});
for await (const textPart of textStream) {
process.stdout.write(textPart);
}
```
The `onAbort` callback receives:
- `steps`: An array of all completed steps before the abort
You can also handle abort events directly in the stream:
```ts highlight="11-14"
import { streamText } from 'ai';
__PROVIDER_IMPORT__;
const { stream } = streamText({
model: __MODEL__,
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
for await (const chunk of stream) {
switch (chunk.type) {
case 'abort': {
// Handle abort directly in stream
console.log('Stream was aborted');
break;
}
// ... handle other part types
}
}
```