When should an API return 422 vs 400?
Return 400 when the HTTP request or JSON syntax itself is broken, and 422 when the JSON is valid but field values violate schema or business rules.
HTTP 422 Unprocessable Content means the server understood the Content-Type and parsed the syntax (valid JSON), but the payload failed semantic or schema validation (e.g., invalid email, negative quantity, Zod/Pydantic error).
HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json
{
"type": "https://api.example.com/errors/validation-failed",
"title": "Validation Failed",
"status": 422,
"errors": [
{ "field": "email", "message": "Must be a valid email address" },
{ "field": "quantity", "message": "Must be >= 1" }
]
}
curl -i -sS -X POST -H 'Content-Type: application/json' -d '{"email":"bad"}' https://api.example.com/v1/signup
Return 400 when the HTTP request or JSON syntax itself is broken, and 422 when the JSON is valid but field values violate schema or business rules.
FastAPI uses Pydantic to validate request bodies, query params, and path params, automatically emitting 422 with a `detail` array showing exact field locations.
No. Display field-level error messages to the user and fix the request payload before resubmitting.