Skip to content

Enforce the chat request byte limit while streaming - #120

Merged
markeljan merged 1 commit into
mainfrom
codex/bound-chat-request-bytes
Nov 15, 2026
Merged

markeljan merged 1 commit into
mainfrom
codex/bound-chat-request-bytes

Conversation

@markeljan

Copy link
Copy Markdown
Owner

Summary

  • Count actual request bytes while reading POST /api/chat, before decoding or parsing an oversized chunk. On overflow, request cancellation and return the existing 413 response without waiting for the source.
  • Preserve the declared-length fast path, the auth and rate-limit order, and UTF-8 decoding across chunk boundaries.

Verification

  • On main at f8e793e, three regression cases failed. ASCII overflow drained the stream, and valid UTF-8 bodies over the cap reached the scripted model with either no declared length or an underreported length. All three pass with this patch.
  • The route tests use the real AI SDK validator and a scripted model. Exactly 1,000,000 bytes preserve a split emoji into the model input. A pending cancellation still returns 413 after the first overflowing byte and the model receives zero calls.
  • Against local Next.js 16.4.6, an HTTP/1.1 chunked upload of 1,000,001 bytes returned 413 before the client ended the request. Chrome confirmed the existing 400 response for malformed JSON and 413 for an oversized request. Redis and the upstream model endpoint were local fixtures.
  • Bun 1.4.2 typecheck, all 310 tests, and lint on the changed files passed. Full local lint flagged the baseline condition in app/chat/[id]/page.tsx. Hosted CI runs the frozen install and full lint.

Count UTF-8 bytes before decoding each chunk so oversized uploads are cancelled before JSON parsing or model dispatch.
@vercel

vercel Bot commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
web3gpt Ready Ready Preview Nov 15, 2026 1:25pm UTC

Request Review

@markeljan
markeljan marked this pull request as ready for review November 15, 2026 13:29
@markeljan
markeljan merged commit ae47647 into main Nov 15, 2026
3 checks passed

This branch was successfully deployed

1 active deployment
Preview — e3590136 Deployed Nov 15, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant