Implement a FastAPI file upload endpoint with form data
Tests FastAPI multipart literacy. A strong answer names python-multipart, uses Annotated[UploadFile, File()] for the image, and Annotated[str, Form()] for user_id.
WHAT THIS TESTS: The interviewer wants to see that you know FastAPI routes incoming request data based on type metadata, not just raw Python types. Specifically they care whether you understand that a file upload plus form fields requires multipart form-data parsing, which FastAPI delegates to python-multipart. They also want to see that you distinguish between in-memory bytes and UploadFile for larger or spooled files, and that you know the exact import pattern using Annotated.
A GOOD ANSWER COVERS: First, state that you must install python-multipart because FastAPI relies on it for multipart parsing. Second, import File and Form from fastapi alongside UploadFile. Third, show the function signature using Annotated: Annotated[UploadFile, File()] for the profile picture and Annotated[str, Form()] for the user_id. Fourth, explain the difference between bytes and UploadFile: bytes loads the entire file into memory, while UploadFile provides a SpooledTemporaryFile, a file name, and a content type, making it safer for images above a few megabytes. Fifth, mention that the endpoint should be async so you can await file read operations without blocking the event loop.
COMMON WRONG ANSWERS: A major red flag is writing the parameter as user_id: str or file: UploadFile without the File() or Form() metadata. Without those wrappers FastAPI assumes the data arrives as JSON in the request body, which will break a multipart upload. Another red flag is forgetting python-multipart entirely and treating the file as a base64 string inside a JSON payload, which is inefficient and non-idiomatic. Candidates also stumble by using standard Pydantic models for the file field, which does not work for raw binary uploads in FastAPI.
LIKELY FOLLOW-UPS: The interviewer may ask how you would validate file size or content type before processing. They might ask how to handle multiple file uploads at once, which is done with a list of Annotated[UploadFile, File()]. They could also ask about streaming large files to external storage like S3 rather than reading everything into memory, or how you would write tests for this endpoint using TestClient with files and data dictionaries.
ONE CONCRETE EXAMPLE: You would write: from typing import Annotated; from fastapi import FastAPI, File, Form, UploadFile; app = FastAPI(); @app.post("/profile") async def update_profile(file: Annotated[UploadFile, File()], user_id: Annotated[str, Form()]): return {"filename": file.filename, "user_id": user_id}. Before running this, you install python-multipart. The File() metadata tells FastAPI to pull the binary from the multipart stream, while Form() pulls the text field, keeping both out of the JSON body parser.
Read the original → fastapi.tiangolo.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.