Skip to content

Comments

fix(api): resolve HTTP 413 on file uploads larger than 1 MB#204

Open
teamauresta wants to merge 1 commit intoHKUDS:devfrom
teamauresta:fix/multipart-upload-413-large-files-v2
Open

fix(api): resolve HTTP 413 on file uploads larger than 1 MB#204
teamauresta wants to merge 1 commit intoHKUDS:devfrom
teamauresta:fix/multipart-upload-413-large-files-v2

Conversation

@teamauresta
Copy link

Description

Fixes HTTP 413 errors when uploading files larger than 1 MB to an existing knowledge base.

Related Issues

Fixes #170

Root Cause

Starlette's MultiPartParser (≥ 0.31) caps individual file parts at 1 MB by default via the class-level max_file_size attribute. Files exceeding this are rejected at the framework layer — before the route handler runs — producing a 413 even when the file is well within DocumentValidator.MAX_FILE_SIZE (100 MB). This is why a 32-byte .txt succeeds while a 2 MB .pdf fails.

Changes Made

src/api/main.py

  • Added a startup block that patches starlette.formparsers.MultiPartParser.max_file_size to 100 MB, matching the application-level cap in DocumentValidator
  • Wrapped in try/except (ImportError, AttributeError) for compatibility with older Starlette versions
  • Includes full inline explanation with reference to this issue and the Starlette source

Checklist

  • Changes tested locally
  • Code reviewed
  • Documentation updated (if necessary)
  • Unit tests added (if applicable)

Additional Notes

DocumentValidator's streaming size check still enforces the real 100 MB per-file limit — this patch simply prevents Starlette from rejecting valid uploads before they reach the route handler.

Starlette's MultiPartParser caps individual file parts at 1 MB by default
(via the class-level max_file_size / max_part_size attribute). Files
larger than this limit are rejected before the route handler runs, which
produces an HTTP 413 even when the file is well within the application-
level 100 MB cap enforced by DocumentValidator.

Patch MultiPartParser.max_file_size to 100 MB at application startup so
that Starlette streams larger uploads through to the route handler, which
then applies the real per-file constraint via DocumentValidator.

The try/except guard keeps compatibility with older Starlette versions
that do not expose this attribute.

Fixes HKUDS#170
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