next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Large HTTP body truncated in API route (using vercel-ai-sdk)

Open oeken opened this issue 1 month ago • 5 comments

Link to the code that reproduces this issue

https://github.com/needle-ai/needle-mcp

To Reproduce

Use Vercel AI SDK's useChat to stream messages. It makes an HTTP request to /api/chat. Request body gets large (around 10kb) since we add many things into the context. However the root cause is not AI SDK since we see the same error for other API routes that dont use AI SDK but have a large payload.

Requests fail with the following error message

SyntaxError: Unexpected token 'a', "are as a S"... is not valid JSON
    at JSON.parse (<anonymous>)
    at g (/home/bun/app/.next/server/chunks/my-app_260fae17._.js:46:2376)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async /home/bun/app/.next/server/chunks/my-app_260fae17._.js:46:902
    at async /home/bun/app/.next/server/chunks/my-app_4aac2b5c._.js:2785:9624
    at async r$.do (/home/bun/app/node_modules/next/dist/compiled/next-server/app-route-turbo.runtime.prod.js:23:20293)
    at async r$.handle (/home/bun/app/node_modules/next/dist/compiled/next-server/app-route-turbo.runtime.prod.js:23:25083)
    at async u (/home/bun/app/.next/server/chunks/my-app_260fae17._.js:46:5991)
    at async r$.handleResponse (/home/bun/app/node_modules/next/dist/compiled/next-server/app-route-turbo.runtime.prod.js:19:73806)
    at async c (/home/bun/app/.next/server/chunks/my-app_260fae17._.js:46:7029)

I traced the line, and figured out that NextRequest.json() is failing due to malformed json body, I examined and saw that it's partial json.

This issue started happening immediately after we updated from NextJS 14 to 16.

Current vs. Expected behavior

Current behaviour: Payload is truncated. Expected behaviour: Request body is not truncated.

Provide environment information

1. Build in `standalone` mode (deployed in GCP VM)
2. `bun` version: 1.2.12
3. `node` version: 23
4. `next` version: 16.0.1

Which area(s) are affected? (Select all that apply)

Dynamic Routes

Which stage(s) are affected? (Select all that apply)

Other (Deployed)

Additional context

No response

oeken avatar Nov 05 '25 13:11 oeken

Update: I've drilled down into the issue and realized the truncated body issue happens only when I use the middleware/proxy in nodejs mode. When I switch back to the edge mode the problem disappears.

I am not sure what would be the intended difference between these 2 if i am targeting such deployment (standalone, docker).

But the takeaway message is edge works fine while nodejs not.

oeken avatar Nov 07 '25 15:11 oeken

Hi everyone,

On my side, I've encountered the same JSON.parse problem but with server actions. The only way for me to fix it was to switch back to middleware.ts file instead of proxy.ts in order to switch back to edge runtime. The problem disappeared after that.

I'm running Next.js version 16.0.3.

DiAifU avatar Nov 18 '25 15:11 DiAifU

Wanted to echo the comments above, just for visibility. Same thing on my end, trying to use server actions with proxy, large response body gets truncated and fails to parse. Middleware set to edge runtime works, but I'd like to use latest config if possible.

akumajoe avatar Nov 24 '25 18:11 akumajoe

Same issue.

In my case, I tracked it down to following:

export async function GET(request: Request) {
  // load ret from upstream server
  // retBody is a string of length 5592
  return new Response(retBody, ...);
}
// response shows up client truncated length ~1kb

...and the issue was ultimately that the response we're proxying has content-length header causing Next to truncate the response.

If you are proxying an upstream response: remove content-encoding and content-length to avoid this problem.

dcposch avatar Nov 29 '25 06:11 dcposch

Is it fixed by this PR ? I believe it is, but I haven't tested it yet.

DiAifU avatar Dec 04 '25 17:12 DiAifU