Vercel · sdk · fg-0239

AI SDK streamText error never reaches try/catch and the chat UI only shows 'An error occurred.'

An error occurred.

Verified 2026-10-15 · Confirmed by 0 builders · Trust 50/100

Cause

streamText returns immediately and provider errors happen later while the stream is consumed, so they become error parts in the stream instead of exceptions. The UI response helpers mask the message by default so details do not leak to clients.

Fix

Pass onError to streamText for logging, and pass an onError formatter to the UI stream response if the client should see the reason.

Apply

const result = streamText({
  model,
  messages,
  onError({ error }) { console.error('streamText failed', error); },
});
return result.toUIMessageStreamResponse({
  onError: (error) => (error instanceof Error ? error.message : 'Unknown error'),
});

Affects: AI SDK (ai package) 4.x through 7.x, still present in ai 7.0.142; streamText onError exists since ai 4.1.22

Sources

Have your agent know this before it fails

claude mcp add --transport http footgun https://getfootgun.com/api/mcp