Skip to main content

Error Handling and Warnings

Warnings

The AI SDK shows warnings when something might not work as expected. These warnings help you fix problems before they cause errors.

When Warnings Appear

Warnings are shown in the browser console when:
  • Unsupported features: You use a feature or setting that is not supported by the AI model (e.g., certain options or parameters).
  • Compatibility warnings: A feature is used in a compatibility mode, which might work differently or less optimally than intended.
  • Other warnings: The AI model reports another type of issue, such as general problems or advisory messages.

Warning Messages

All warnings start with “AI SDK Warning:” so you can easily find them. For example:

Turning Off Warnings

By default, warnings are shown in the console. You can control this behavior:

Turn Off All Warnings

Set a global variable to turn off warnings completely:

Custom Warning Handler

You can also provide your own function to handle warnings. It receives provider id, model id, and a list of warnings.

Error Handling

Error Helper Object

Each AI SDK UI hook also returns an error object that you can use to render the error in your UI. You can use the error object to show an error message, disable the submit button, or show a retry button.
We recommend showing a generic error message to the user, such as “Something went wrong.” This is a good practice to avoid leaking information from the server.

Alternative: replace last message

Alternatively you can write a custom submit handler that replaces the last message when an error is present.

Error Handling Callback

Errors can be processed by passing an onError callback function as an option to the useChat or useCompletion hooks. The callback function receives an error object as an argument.

Injecting Errors for Testing

You might want to create errors for testing. You can easily do so by throwing an error in your route handler: