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
- Set response_model so sensitive fields such as passwords are never returned
- Choose status codes like 201 for creation and 204 for no content
- Raise HTTPException with a status code and a detail message
- Return files, redirects and streaming responses with the matching response classes
- Describe additional responses so they appear in the OpenAPI schema
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.
Other units in this stage
Back to the full FastAPI roadmap