Error Handling
Handling Regular Errors
Regular errors are thrown and can be handled using thetry/catch block.
Handling Streaming Errors (Simple Streams)
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 thetry/catch block.
Handling Streaming Errors (Streaming with error Support)
Full streams support 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.
Handling Stream Aborts
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 theonAbort callback to handle these cases.
The onAbort callback is called when a stream is aborted via AbortSignal, but onFinish is not called. This ensures you can still update your UI state appropriately.
onAbort callback receives:
steps: An array of all completed steps before the abort
Common Error Types
The AI SDK provides several error types to help you handle different failure scenarios:API Errors
APICallError: Thrown when an API call failsInvalidResponseDataError: Thrown when the API response is invalidEmptyResponseBodyError: Thrown when the API returns an empty response
Content Generation Errors
NoContentGeneratedError: Thrown when no content is generatedNoOutputGeneratedError: Thrown when no output is generatedNoImageGeneratedError: Thrown when no image is generatedNoSpeechGeneratedError: Thrown when no speech is generatedNoTranscriptGeneratedError: Thrown when no transcript is generatedNoVideoGeneratedError: Thrown when no video is generated
Tool Errors
NoSuchToolError: Thrown when a tool is not foundInvalidToolInputError: Thrown when tool input is invalidMissingToolResultsError: Thrown when tool results are missing
Configuration Errors
LoadAPIKeyError: Thrown when loading an API key failsNoSuchModelError: Thrown when a model is not foundUnsupportedFunctionalityError: Thrown when a feature is not supported
Validation Errors
TypeValidationError: Thrown when type validation failsInvalidArgumentError: Thrown when an argument is invalid
instanceof checks to handle specific error types: