## Summary

When a Next.js App Router chat built with the Vercel AI SDK (`useChat` + `streamText`) fails with `Failed to parse stream`, `An error occurred`, or a `504` timeout in production, the issue is usually a mismatched `toDataStreamResponse()` vs. `toTextStreamResponse()`, missing `maxDuration`, or unforwarded upstream errors.

If you want this fixed cleanly, SazM repairs broken Next.js and Vercel AI SDK streaming routes for a **fixed $99, delivered within 48 hours**, 100% in writing with zero calls. See our [AI integration services](/ai-integration-services) or [request the $99 fix](/contact?offer=emergency).

## 1. Match `useChat` Protocol to `toDataStreamResponse()`

- `useChat` expects the Data Stream protocol (`result.toDataStreamResponse()`), not plain text (`toTextStreamResponse()`), unless `streamProtocol: 'text'` is explicitly set on the client.
- Pass `getErrorMessage` to `toDataStreamResponse()` so real upstream API errors surface in logs instead of a generic `"An error occurred"`.

## 2. Export `maxDuration` in App Router

- Add `export const maxDuration = 60;` (or `edge` runtime) to `app/api/chat/route.ts` so serverless platforms do not kill long-running streams at 10 seconds.

## Need It Fixed?

We debug production Next.js, TypeScript, OpenAI/Anthropic, and Cloudflare Workers AI streaming apps—plus our 5-tool MCP server (`sazm.in/mcp`)—for a **fixed $99 within 48 hours**, verified before you pay and 100% async. [Start your $99 fix](/contact?offer=emergency) or explore our [AI integration services](/ai-integration-services).
