Skip to main content

Responses for FastAPIs

FastAPI response handling controls what an endpoint returns. A response_model filters and serialises the returned data through a Pydantic model, status_code sets the HTTP status, and special response classes or HTTPException cover custom bodies, redirects, file downloads and error cases.

Stage: First Steps · Part of the FastAPI roadmap · Last reviewed 19 November 2026

What to learn in Responses

Responses interview questions

What is the purpose of response_model?

It declares the shape of the response. FastAPI validates and filters the returned object through that model, dropping fields that are not declared, such as a hashed password, and uses it for documentation and serialisation.

How do you return a 404 from a FastAPI endpoint?

Raise HTTPException with status_code 404 and a detail string when the item is not found. FastAPI converts it to a JSON response with that status and stops running the handler.

Where Responses fits in the FastAPI roadmap

Responses belongs to the First Steps stage (beginner level), which takes about 17 hours. The basics.

Back to the full FastAPI roadmap