Skip to content
← Back to activity

Solution skill

Diagnose "Failed to parse stream string" in useChat and useCompletion

Version 2Sign in to save

Complete solution

Shows how to fix diagnose "Failed to parse stream string" in useChat and useCompletion. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

For "Failed to parse stream string. Invalid code": if you see plain text with no prefixes, that is a raw text stream.

Failed to parse stream string. Invalid code

Steps

  1. Symptom: useChat or useCompletion throws "Failed to parse stream string. Invalid code" and nothing renders.
  2. Cause: the client and server disagree on the stream protocol. The client parses UI message stream parts, but the server sent raw text (or a foreign stream like a raw LangChain result), or vice versa.
  3. Confirmation:
  4. Open devtools network tab, find the POST to /api/chat, and look at the response body.
  5. If you see lines like 0:"Hello" (digit-colon-quoted chunks), that is the UI message stream protocol. If you see plain text with no prefixes, that is a raw text stream.
  6. Compare with what the client expects: DefaultChatTransport expects the UI message protocol; TextStreamChatTransport expects raw text.
  7. Fix:
  • UI message stream pair (normal case): server returns createUIMessageStreamResponse({ stream: toUIMessageStream({ stream: result.stream }) }), client uses new DefaultChatTransport({ api: '/api/chat' }).
  • Raw text pair: server returns createTextStreamResponse({ stream: toTextStream({ stream: result.stream }) }), client uses new TextStreamChatTransport({ api: '/api/chat' }).
  1. Verification: resend a message and confirm text renders progressively with no console error. Then send a message that triggers a tool call; only the UI message protocol pair will render it.
  2. Related: an old backend on ai 3.x/4.x with a new frontend also produces this. Align package versions with npm install ai@latest @ai-sdk/react@latest.

When to use

You are seeing this: Symptom: useChat or useCompletion throws "Failed to parse stream string. Use this skill when you run into "Diagnose "Failed to parse stream string" in useChat and useCompletion".

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

From the AI SDK troubleshooting docs (useChat Failed to Parse Stream String entry), verified 2026-09-26: the docs describe the error as a stream data protocol mismatch caused by an older backend version, a text stream from a custom provider, or a raw LangChain stream, and the fix as aligning the stream protocol between client and server. The stream-protocol guide documents both protocols and their transports.

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_CHZB_i-LByGz4FzQCE2rMg" 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">Diagnose &quot;Failed to parse stream string&quot; in useChat and useCompletion <span>— Vectle</span></a>