FastAPI File Uploads and Form Data
Master multipart/form-data in FastAPI.
- Use UploadFile over bytes for streaming, memory efficiency, and metadata access
- FastAPI parses multipart data into individual Form() parameters — no single Pydantic model for the whole body
- UploadFile wraps a SpooledTemporaryFile: small files stay in memory, large files hit disk automatically
- Always validate content_type and enforce file size before writing to disk
- The biggest production trap: trusting client-provided MIME type — verify with python-magic or inspect file headers
Uploading a Single File with Validation
The UploadFile class is a wrapper around a Python SpooledTemporaryFile. This is critical for performance: it keeps small files in RAM for speed but offloads larger files to the temporary directory of your OS to prevent memory exhaustion. In production, always validate the content_type and enforce a maximum file size.
file.size attribute is only available in Starlette 0.20+ — always implement a manual size check as a fallback.await file.read() without arguments.UploadFile for streaming, but never trust content_type alone.Mixing Form Fields with File Uploads
A frequent pain point for developers is trying to send a Pydantic JSON body alongside a file. Due to the way HTTP works, you cannot easily mix application/json and multipart/form-data. Instead, you must declare each form field using Form(). FastAPI will then parse the multipart body and map the keys to your arguments.
metadata field) — this breaks type safety.Form() for each field.None and use UploadFile | None = File(None).Form(...) — FastAPI will parse it from the multipart body.UploadFile = File(...) — gives streaming, metadata, and efficient handling.Form(), then deserialize in the endpoint.Handling Multiple File Uploads
FastAPI natively supports multiple files under the same field name by declaring the parameter as a list of UploadFile. The client sends each file with the same key, and FastAPI collects them into a Python list. This is common for batch uploads, image galleries, or attachment-heavy workflows.
content += chunk) before writing. For large files, this defeats the purpose of streaming. Use proper streaming by writing each chunk directly to a file descriptor instead of accumulating in a bytes object.asyncio.gather for concurrent file processing, but watch for I/O contention on disk.list[UploadFile] for multiple files.MAX_FILES and MAX_TOTAL_SIZE before processing.Validating File Type Beyond Content-Type
The content_type attribute from the client is trivial to spoof. A malicious client can upload a .exe with Content-Type: image/jpeg. To validate file contents, you need to inspect file signatures (magic bytes). Use python-magic which wraps libmagic, or manually check the first few bytes. This is critical for security-sensitive uploads like profile pictures or document scans.
- Magic bytes are the first n bytes of a file — they identify the format regardless of extension or MIME type.
- libmagic (via python-magic) reads these bytes and returns the actual MIME type.
- Always seek back to 0 after reading the magic chunk — you consumed part of the stream.
- For images, you can also use PIL (Pillow) to verify the image can be decoded — this catches truncated or corrupted files.
content_type directly leads to security vulnerabilities. Attackers can upload executable files disguised as images.python-magic adds latency per upload (~5ms). For high-throughput endpoints, cache allowed MIME signatures or use a file extension whitelist as a pre-filter.content_type is client-controlled — never trust it for security decisions.python-magic reads actual file content to determine MIME type.seek(0) after reading magic bytes to not lose the file data.python-magic or manual header check. Must verify before any processing.content_type plus extension check — but still log mismatches for audit.Sanitising Filenames to Prevent Path Traversal
Saving uploaded files with the client-provided filename is dangerous. A malicious user can supply a name like ../../etc/passwd to overwrite system files or create files outside the intended directory. Always sanitize filenames: strip directory separators, use a whitelist of allowed characters, or generate a UUID-based name and store the original in a database.
os.path.basename and join with a safe base directory.OOM Kill in Production: The 10MB File That Brought Down the API
kubectl top pods showed memory usage grew linearly with file size. Eventually the pod was OOMKilled and restarted.UploadFile would handle large files by streaming them to disk automatically, so no explicit chunked reading was implemented in the endpoint.await file.read() without a size limit, reading the entire file into memory before processing. UploadFile's spooling only kicks in if you write to disk via file.file; read() returns all bytes at once.file.file for streaming writes with shutil.copyfileobj().- Never call
awaitwithout a size argument in production — you're asking for an OOM.file.read() - Always enforce a maximum file size upfront, before processing any content.
- Use
file.file(the SpooledTemporaryFile) for streaming writes, notfor large files.read()
client_max_body_size or similar setting. FastAPI itself doesn't impose a limit unless you code it, but proxies do.read() for validation and then again for saving). Use file.file.seek(0) after first read.File parameter is always None even though file was sentmultipart/form-data and the field name matches the endpoint parameter. Use curl -F 'file=@/path' to test.Form(...) (not Body()). Mixing JSON and multipart is not allowed; use separate Form() parameters.FILE_MAX_SIZE and reject early. Use asyncio.Semaphore to limit concurrent uploads.UploadFile = File(...)Key takeaways
Form() or use a library like python-multipart.python-magic or check the file header if you need deep validation of file types beyond the client-provided MIME type.Common mistakes to avoid
5 patternsCalling `await file.read()` without a size argument
while chunk := await file.read(1024 * 1024):Using `File()` for form fields that are not files (e.g., strings)
Form(...) for non-file form fields. Only use File(...) for actual file upload parameters.Saving file directly with client-provided filename
Assuming `content_type` is accurate
python-magic to detect actual MIME type from file bytes.Not resetting file stream position after reading
await file.read() or file.file.read() returns empty bytes.await file.seek(0) after any read call that you expect to re-read.Interview Questions on This Topic
Explain the difference between `bytes` and `UploadFile` in a FastAPI endpoint. Which one would you use for a 2GB video upload and why?
bytes reads the entire file into memory; UploadFile wraps a SpooledTemporaryFile that streams content. For a 2GB video, use UploadFile because it allows chunked reading (e.g., in 1MB blocks), avoiding OOM. Also gives immediate access to filename and content_type without reading the body.Frequently Asked Questions
That's Python Libraries. Mark it forged?
3 min read · try the examples if you haven't