What this thread does
Do first: replace the upload route's JSON Body expectation with Form() metadata and File() parameters carried in one multipart request.
Short answer
As of November 2026, FastAPI 0.165.0 follows the forms/files contract: a multipart upload cannot simultaneously supply a top-level application/json body. A string can be one form field and then be validated as structured data.
Diagnostic: 422
Inspect request validation locations and field types before changing response serialization (validation handling).
Choose / don't choose
- Separate Form fields: choose for small flat metadata with ordinary field validation; don't serialize a nested object unless necessary.
- JSON-valued Form string: choose for nested metadata plus a file in the same request; parse/validate explicitly using
model_validate_json()(Pydantic). - Separate JSON request: choose when metadata has an independent lifecycle or needs its own JSON API; design association and orphan-upload cleanup explicitly.
Not for: general request-shape diagnostics; see fastapi-422-unprocessable-entity-read-the-loc-and-fix-request-shape.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
File() / UploadFile | Example file: Annotated[UploadFile, File()]; field name is file | Current FastAPI docs | forms/files |
Form() | Example metadata: Annotated[str, Form()]; field name metadata is this app's choice | Current FastAPI parameter contract | forms/files |
Content-Type | multipart/form-data; the browser sets its boundary expression, so don't manually override it | Browser multipart request | FormData |
model_validate_json() | Validate the Form string against the app's metadata model before accepting it | Current Pydantic JSON API | JSON |
close() | await after using the upload, including rejection/cleanup paths | Current FastAPI file-method docs | files |
Order of operations
1. Install the multipart parser using the documented uv add python-multipart; declare the file and Form parameters (forms/files). 2. Construct multipart parts named exactly file and metadata for the illustrative declarations above. Append the actual File/Blob and a string to FormData; let the browser encode its boundary (FormData). 3. Parse and validate the metadata string with the application's Pydantic model via model_validate_json(). Catch ValidationError; convert it into a controlled client error, for example HTTPException(status_code=422, detail="Invalid metadata"). That detail is application-defined (JSON, HTTP errors). 4. Only after schema validation, authorization and file-policy checks, move/copy the upload into permanent storage. Treat this ordering as application policy: UploadFile uses a "spooled" file. Call close() in cleanup, and discard any rejected temporary artifact (files). 5. Exercise valid file/metadata, malformed JSON, invalid metadata types and a wrong part name. Invalid metadata must not create a permanent record/file.
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|
Pythonweb2's November 4, 2022 discussion documents the structured-metadata use case. Its older helper code is historical evidence, not a current Pydantic API recipe.
Missing
- Upload size/content checks and transactional coordination with permanent storage depend on the service. A JSON string in a multipart part is not a second JSON request body.
Checked
2026-11-24; FastAPI release, forms/files/errors, Pydantic JSON, browser FormData and dated discussion read. No upload endpoint commands executed.
Started from a post by @FastAPI, packaged by the editor. Original post