Skip to content

Instantly share code, notes, and snippets.

@johnidm
Created September 23, 2025 14:43
Show Gist options
  • Select an option

  • Save johnidm/1d28456a3ccbaba281f751825a1a4c81 to your computer and use it in GitHub Desktop.

Select an option

Save johnidm/1d28456a3ccbaba281f751825a1a4c81 to your computer and use it in GitHub Desktop.
Upload Memory Error - FastAPI

🔹 1. Use streaming instead of loading everything into memory

Instead of await file.read(), you can save the file temporarily to disk and then process it:

import tempfile
from fastapi import UploadFile, HTTPException, status

async def process_file(file: UploadFile, mode: str):
    if file.content_type != "application/pdf":
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=f"File is not a PDF. File type: {file.content_type}",
        )

    # create temporary file
    with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
        while chunk := await file.read(1024 * 1024):  # read in 1MB chunks
            tmp.write(chunk)
        tmp_path = tmp.name

    document = extract_metadata(tmp_path, file.filename, mode)  
    return document

🔹 2. Process directly via SpooledTemporaryFile (no manual file writing)

FastAPI’s UploadFile is already based on SpooledTemporaryFile, so you can use file.file directly (without calling .read()):

async def process_file(file: UploadFile, mode: str):
    if file.content_type != "application/pdf":
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=f"File is not a PDF. File type: {file.content_type}",
        )

    # pass the file object directly (no full in-memory load)
    document = extract_metadata(file.file, file.filename, mode)  
    return document    

This avoids loading the entire PDF into memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment