fix(api_server): return 413 for oversized chunked bodies

api_server already caps every read via client_max_size (chunked
included), but when the limit tripped mid-read the handler's broad JSON
except turned it into 400 'Invalid JSON'. Catch
HTTPRequestEntityTooLarge in body_limit_middleware and return the
OpenAI-style 413.

Status-code polish extracted from PR #3949 by @Gutslabs — the PR's core
client_max_size change already exists on main.
This commit is contained in:
Gutslabs 2026-07-04 15:35:05 -07:00 committed by teknium1
parent ec29590a0f
commit 2b4ec0082a
No known key found for this signature in database

View file

@ -669,7 +669,16 @@ if AIOHTTP_AVAILABLE:
return web.json_response(_openai_error("Request body too large.", code="body_too_large"), status=413)
except ValueError:
return web.json_response(_openai_error("Invalid Content-Length header.", code="invalid_content_length"), status=400)
return await handler(request)
try:
return await handler(request)
except web.HTTPRequestEntityTooLarge:
# aiohttp's client_max_size tripped mid-read (chunked bodies carry
# no Content-Length) — return a proper 413 instead of letting the
# handler's broad JSON except turn it into 400 "Invalid JSON".
return web.json_response(
_openai_error("Request body too large.", code="body_too_large"),
status=413,
)
else:
body_limit_middleware = None # type: ignore[assignment]