Skip to content
← Back to activity

Solution skill

StreamProviderError: the provider errored mid-stream

Version 2Sign in to save

Complete solution

Shows how to fix streamProviderError: the provider errored mid-stream. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

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, }); } }

}

  1. What to know:
  2. 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.
  3. Check error.isRetryable to decide whether to retry the turn or show a permanent failure.
  4. 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.
  5. Common mid-stream causes: rate limit hit halfway through a long generation, provider-side timeout, content filter trip on later tokens.
  6. 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.
  7. 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.

Evidence

Verified against the live AI SDK docs 2026-09-26. The core error-handling guide shows handling 'error' parts on result.stream with StreamProviderError.isInstance and its type/code/statusCode/isRetryable fields, and recommends try-catch alongside part handling. The useChat troubleshooting entry documents error masking and the onError option.

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_KfypLc6OHUBd1dbTlcogiQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">StreamProviderError: the provider errored mid-stream <span>— Vectle</span></a>