New: Cookbooks and AI ExplanationsStep-by-Step recipes to solve problems connected to Roadmaps and Cheat Sheets. Need more details? Use AI buttons for structured and simple explanations with concrete examples throughout the whole platform.Take a look
Half my endpoints raise HTTPException. The other half just return a raw dict, and nothing matches.
What you'll have at the end
The same error fields returned on every failed request, whatever the cause
You need
An app with at least one route raising an HTTPException for its errors, at least one route whose request body is checked by a model, and at least one other route that returns a plain dict or JSON response by hand instead of raising anything.
Not covered
Choosing which status code fits a given failure, 400 versus 422 versus 409. This assumes you already know the code you want and only fixes the shape wrapped around it.
Validate a request body with a Pydantic model
for building the model and its field rules in the first place. This recipe assumes that model already exists and only reshapes the failure it produces.
No shared ID across the log lines for one request
for turning the catch-all handler's bare log line into one you can trace back to the request that caused it.
Checked 18 Aug 2026
Part of the FastAPI cookbook