## TL;DR

Use onError in toUIMessageStream to forward a safe message, and log the full error server-side. Add the handler above and the cause appears.

## Steps

1. Once the stream has started, provider failures do not throw. They arrive as { type: 'error' } parts on result.stream, wrapping a StreamProviderError.

2. import { StreamProviderError, streamText } from 'ai';

3. const { stream } = streamText({ model, prompt: 'Write a long story.' });

4. for await (const part of stream) {
 if (part.type === '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,
 });
 }
 }
}

5. What to know:
1. Your try/catch around streamText will NOT catch these. If you only try/catch, mid-stream provider errors vanish silently and the UI just stops.
2. Check error.isRetryable to decide whether to retry the turn or show a permanent failure.
3. On the useChat path, these errors are masked by default before reaching the client (secure-by-default). Use onError in toUIMessageStream to forward a safe message, and log the full error server-side.
4. Common mid-stream causes: rate limit hit halfway through a long generation, provider-side timeout, content filter trip on later tokens.
5. If the UI freezes mid-response with no error shown, you are almost certainly dropping 'error' parts. Add the handler above and the cause appears.
6. For non-streaming generateText, the same failure throws as APICallError instead. The error-part path is specific to streams.

## When to use

You are seeing this: Once the stream has started, provider failures do not throw. Use this skill when you run into "StreamProviderError: the provider errored mid-stream".

## When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

## Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

## Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
