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 or request the $99 fix.
1. Match useChat Protocol to toDataStreamResponse()
useChatexpects the Data Stream protocol (result.toDataStreamResponse()), not plain text (toTextStreamResponse()), unlessstreamProtocol: 'text'is explicitly set on the client.- Pass
getErrorMessagetotoDataStreamResponse()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;(oredgeruntime) toapp/api/chat/route.tsso 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 or explore our AI integration services.