Python4 min read2026-03-01
FastAPI 422 Unprocessable Entity
Understand and fix HTTP 422 validation errors generated by Pydantic request body validations in FastAPI.
Error Code / Stack Trace
HTTP/1.1 422 Unprocessable Entity
{"detail": [{"loc": ["body", "email"], "msg": "field required", "type": "value_error.missing"}]}Problem Overview
FastAPI automatically rejects the client HTTP request because the JSON body, query parameter, or path variable failed Pydantic schema validation.
Why Does This Happen?
- Missing a required field in the JSON payload sent by the frontend.
- Data type mismatch (e.g. sending a string 'hello' for an integer id field).
- Forgetting to set the 'Content-Type: application/json' header in the client request.
Step-by-Step Solution
Step 1: Inspect the exact detail array in the 422 response
FastAPI tells you the exact field and error in the 'detail' JSON array.
bash
curl -X POST http://localhost:8000/items -H 'Content-Type: application/json' -d '{"title": "Item 1", "price": 19.99}'Step 2: Make fields optional in Pydantic schema if not required
Use Optional[T] = None in Pydantic models for non-mandatory fields.
python
from pydantic import BaseModel
from typing import Optional
class ItemCreate(BaseModel):
name: str
description: Optional[str] = None # Optional field
price: floatCommon Mistakes to Avoid
- •Sending payload as form-data instead of JSON when the endpoint expects a Pydantic model body.
Prevention & Best Practices
- Use FastAPI's interactive Swagger UI at /docs to test request schemas directly.
Frequently Asked Questions
What is the difference between 400 Bad Request and 422 Unprocessable Entity?
400 indicates malformed syntax (e.g. invalid JSON). 422 indicates the syntax is valid JSON, but the data violates semantic validation rules.
Related Developer Solutions & Tools
Recommended Tools
Related Error Fixes