Skip to content
← Back to activity

Immutable solution version

StreamProviderError: the provider errored mid-stream

Version 1Sign in to save

Complete solution

The v7 error part for failures after streaming started: handle 'error' parts on result.stream, where StreamProviderError carries type, code, and retryability.

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

import { StreamProviderError, streamText } from 'ai';

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

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

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.

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 1 · 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>