Skip to content

Enhancement: v3 abort request with 413 instead of 400 if request body is too large #4439

@vaishcodescape

Description

@vaishcodescape

Description

# FIXME (3.0): This should raise a '413 - Request Entity Too Large', but for
# backwards compatibility, we keep it as a 400 for now
raise ClientException("Request Entity Too Large") from None

The correct exception class RequestEntityTooLarge already exists in the codebase and is used elsewhere (e.g., in connection/request.py), but the multipart parser hasn't been updated to use it.

Impact:

  • HTTP specification non-compliance
  • Clients cannot properly differentiate between bad request format (400) and payload too large (413)
  • Inconsistent error handling across the framework

Proposed Solution:

  • Import RequestEntityTooLarge in litestar/_multipart.py
  • Replace ClientException("Request Entity Too Large") with RequestEntityTooLarge
  • Update related tests to expect HTTP 413 instead of 400
  • Mark as breaking change for v3.0 release notes

URL to code causing the issue

https://github.com/litestar-org/litestar/blob/main/litestar/_multipart.py#L136-L138

MCVE

## **MCVE:**ython
from litestarmport Litestar, postom litestar.dataploadFile

@post("/uploastructures import UploadFile

@post("/uploaoad_file(ata: UploadFile) -> dictstr, str]:
    return {"}filename": data.filename}Litestar(rs=[upload_file
    route_handlers=[upload_file_limitSet  # Setow limit to trigger very low limit to trigger a file that exceeds the limit: uploading a file that exceeds the limit:havior: Returnscted behavior: Should Request
# Expected behavior: ShoulTP 413 RequestToo Large

Steps to reproduce

  1. Create a Litestar app with a file upload endpoint
  2. Set multipart_form_part_limit to a low value (e.g., 1)
  3. Upload a multipart form with more parts than the limit allows
  4. Observe the HTTP status code returned
  5. Current: Receives HTTP 400 (Bad Request)
  6. Expected: Should receive HTTP 413 (Request Entity Too Large)

Alternative reproduction:

  1. Navigate to litestar/_multipart.py line 136-138
  2. Observe the FIXME comment acknowledging this issue
  3. Note that RequestEntityTooLarge exception already exists but is not used here

Screenshots

No response

Logs

# Current behavior when multipart limit is exceeded:
# HTTP/1.1 400 Bad Request
# {"detail": "Request Entity Too Large", "status_code": 400}

# Expected behavior:
# HTTP/1.1 413 Request Entity Too Large  
# {"detail": "Request Entity Too Large", "status_code": 413}

Litestar Version

Latest main branch (as of October 2025)

This issue exists in the current main branch and is marked for fixing in version 3.0
per the FIXME comment in the source code.

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🐛This is something that is not working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions