Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

FastAPI answers 422 with Field required and type missing

Your error

Field required [type=missing, input_value={...}, input_type=dict]1 more line it can show

Fix it fast

Most common cause, 1 of 3

An Optional field has no default, so Pydantic 2 still demands the key

Give every field that callers may skip an explicit default.

Fix
from pydantic import BaseModel

class Item(BaseModel):
    name: str
    description: str | None = None  # may be left out
    tax: float | None = None
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Field required [type=missing, input_value={...}, input_type=dict] means Pydantic got a dict without a key the model needs. In FastAPI, loc shows where it looked. Usual reasons: Optional[str] without = None is still required in Pydantic 2, a plain str parameter comes from the query string, or two body models expect nested keys. Applies to Pydantic 2.14, FastAPI 0.165.

  • Applies to Pydantic 2.14, FastAPI 0.165, Python 3.11-3.15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Send the failing request once more and read the loc array in the 422 reply. Its first entry is query or body, and the second names the key FastAPI could not find. That pair points to one of the causes listed here.

Run this first
curl -s -X POST http://127.0.0.1:8000/items \
  -H "Content-Type: application/json" \
  -d '{"name": "Foo", "price": 10}'

Then find the line you see in this list and open its fix:

  1. 1An Optional field has no default, so Pydantic 2 still demands the keyloc is ["body", "<field>"], and that field is written as Optional[X] or X | None with nothing after the type
  2. 2A plain str or int parameter is read from the query string, not the JSON bodyloc starts with "query", even though the client sent the value inside a JSON body
  3. 3Two body models, or Body(embed=True), make FastAPI expect the parameter name as a keyloc is ["body", "item"] or ["body", "user"], naming a function parameter rather than a field inside the model

What does the error look like?

  • Field required [type=missing, input_value={...}, input_type=dict]
  • {"detail":[{"type":"missing","loc":["query","name"],"msg":"Field required","input":null}]}

Cause 1 of 3

An Optional field has no default, so Pydantic 2 still demands the key

You seeloc is ["body", "<field>"], and that field is written as Optional[X] or X | None with nothing after the type

Check

Pydantic 2 reports Field required [type=missing] for a field typed Optional[str] with no default, because version 2 follows dataclass rules: the field may hold None, yet the key itself must be present. In Pydantic 1 the same line quietly defaulted to None, so code moved from V1 starts failing on payloads that skip the key.

Check
grep -nE "Optional\[|\| None" app/*.py | grep -v "="

Fix

Give every field that callers may skip an explicit default. Leave a field without a default only when the caller must always send it, even as null.

Fix
from pydantic import BaseModel

class Item(BaseModel):
    name: str
    description: str | None = None  # may be left out
    tax: float | None = None

Verify

Post the payload without description. FastAPI accepts it and the response shows description as null.

Verify
curl -s -X POST http://127.0.0.1:8000/items -H "Content-Type: application/json" -d '{"name": "Foo", "price": 10}'

Confirmed by pydantic.dev

Did this fix it?

Cause 2 of 3

A plain str or int parameter is read from the query string, not the JSON body

You seeloc starts with "query", even though the client sent the value inside a JSON body

Check

FastAPI raises Field required with loc ["query", ...] when an endpoint argument has a singular type such as str, int or bool, because it reads such arguments from the URL query string. Only a parameter typed as a Pydantic model is taken from the request body, so a JSON body carrying that key is ignored.

Check
grep -nE "^async def|^def" -A2 app/main.py

Fix

Wrap the values in a Pydantic model and use it as the parameter type. For a single loose value, mark it with Body() so FastAPI reads it from the JSON.

Fix
from typing import Annotated
from fastapi import Body, FastAPI
from pydantic import BaseModel

app = FastAPI()

class Login(BaseModel):
    username: str

@app.post("/login")
async def login(data: Login):
    return {"user": data.username}

@app.post("/rate")
async def rate(score: Annotated[int, Body()]):
    return {"score": score}

Verify

Repeat the JSON request. The reply is 200 and the interactive docs at /docs now list the value under Request body instead of Parameters.

Verify
curl -s -X POST http://127.0.0.1:8000/login -H "Content-Type: application/json" -d '{"username": "ana"}'

Confirmed by fastapi.tiangolo.com

Did this fix it?

Cause 3 of 3

Two body models, or Body(embed=True), make FastAPI expect the parameter name as a key

You seeloc is ["body", "item"] or ["body", "user"], naming a function parameter rather than a field inside the model

Check

Once an endpoint takes more than one Pydantic model, or one model marked Body(embed=True), FastAPI stops reading the fields at the top level of the JSON. It expects each parameter name as an outer key holding that model's fields, so a flat payload produces type missing for the parameter itself.

Check
grep -n "embed=True" -r app/

Fix

Nest the payload under the parameter names the endpoint declares, or, when only one model is intended, drop the second body parameter and the embed flag.

Fix
curl -s -X PUT http://127.0.0.1:8000/items/5 \
  -H "Content-Type: application/json" \
  -d '{"item": {"name": "Foo", "price": 10}, "user": {"username": "ana"}}'

Verify

The nested request returns 200 and the handler receives both objects.

Verify
curl -s -o /dev/null -w "%{http_code}\n" -X PUT http://127.0.0.1:8000/items/5 -H "Content-Type: application/json" -d '{"item": {"name": "Foo", "price": 10}, "user": {"username": "ana"}}'

Confirmed by fastapi.tiangolo.com

Did this fix it?

How do I stop it coming back?

  • Write = None next to every X | None field that clients may leave out, and add a test that posts the smallest valid payload.
  • Open /docs after adding an endpoint and confirm each value appears under Parameters or Request body as intended.
  • Keep one body model per endpoint unless the client team has agreed on the nested shape.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.