Part 09 / Topic 4 of 7
Validation and errors
The two halves of the error contract:
4xx: precise and helpful. Pydantic models make FastAPI return 422 with field-level detail automatically — the empty PATCH body above never reached the route function. Your own 4xx messages should name the problem (shipment S-9999 not found), because the person reading them is the customer's engineer at integration time.
5xx: honest but opaque. When your code breaks, the caller gets {"detail": "internal error"} and an id they can quote to you — and the stack trace goes to your logs, not the response. Internals in a 5xx body (paths, queries, library names) are a security leak into systems you do not control.
🧪 Quick quiz
- A PATCH request has a valid key but is missing the required
statusfield. What did FastAPI return?
Show the answer
422 — Pydantic validation failed before the route ran. (verified against the example output above)
Done here? The practice set for Part 9 — 4 exercises with solutions — is on the part page.