FastAPI answers 422 with Field required and type missing
Fix it fast
Most common cause, 1 of 3An Optional field has no default, so Pydantic 2 still demands the key
Give every field that callers may skip an explicit default.
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: str | None = None # may be left out
tax: float | None = NoneShort 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.
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.
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:
- 1An Optional field has no default, so Pydantic 2 still demands the key
loc is ["body", "<field>"], and that field is written as Optional[X] or X | None with nothing after the type - 2A plain str or int parameter is read from the query string, not the JSON body
loc starts with "query", even though the client sent the value inside a JSON body - 3Two body models, or Body(embed=True), make FastAPI expect the parameter name as a key
loc 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.
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.
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: str | None = None # may be left out
tax: float | None = NoneVerify
Post the payload without description. FastAPI accepts it and the response shows description as null.
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
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.
grep -nE "^async def|^def" -A2 app/main.pyFix
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.
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.
curl -s -X POST http://127.0.0.1:8000/login -H "Content-Type: application/json" -d '{"username": "ana"}'Confirmed by fastapi.tiangolo.com
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.
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.
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.
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
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.