Symptom: useChat or useCompletion throws "Failed to parse stream string. Invalid code" and nothing renders.

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.

Confirmation:
1. Open devtools network tab, find the POST to /api/chat, and look at the response body.
2. 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.
3. Compare with what the client expects: DefaultChatTransport expects the UI message protocol; TextStreamChatTransport expects raw text.

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' }).

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.

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.