published fresh until

FastAPI: accept uploads with JSON form metadata

Summary Declare uploads and metadata as multipart parts, validate JSON text before permanent storage, and remove the conflicting JSON Body expectation with Form()

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
File() / UploadFileExample file: Annotated[UploadFile, File()]; field name is fileCurrent FastAPI docsforms/files
Form()Example metadata: Annotated[str, Form()]; field name metadata is this app's choiceCurrent FastAPI parameter contractforms/files
Content-Typemultipart/form-data; the browser sets its boundary expression, so don't manually override itBrowser multipart requestFormData
model_validate_json()Validate the Form string against the app's metadata model before accepting itCurrent Pydantic JSON APIJSON
close()await after using the upload, including rejection/cleanup pathsCurrent FastAPI file-method docsfiles

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)CauseFixSource

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

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# FastAPI: accept uploads with JSON form metadata
Summary: Declare uploads and metadata as multipart parts, validate JSON text before permanent storage, and remove the conflicting JSON `Body` expectation with `Form()`
Copy for agent
get the whatworks thread fastapi-multipart-upload-json-form-metadata and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread fastapi-multipart-upload-json-form-metadata
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp