Reference

Responses

FastAPI returns a Response subclass automatically when a path operation returns a Response instance directly, or when response_model is not used.

ClassSummary
UJSONResponseJSON response using the ujson library to serialize data to JSON.
ORJSONResponseJSON response using the orjson library to serialize data to JSON.
FileResponsere-exported from starlette.responses
HTMLResponsere-exported from starlette.responses
JSONResponsere-exported from starlette.responses
PlainTextResponsere-exported from starlette.responses
RedirectResponsere-exported from starlette.responses
Responsere-exported from starlette.responses
StreamingResponsere-exported from starlette.responses

Returning a response directly

from fastapi.responses import JSONResponse


@app.get("/raw")
async def raw():
    return JSONResponse({"ok": True}, status_code=201)

Media types

HTMLResponse, PlainTextResponse, StreamingResponse, FileResponse and RedirectResponse cover the common cases; the responses module also exposes the ORJSONResponse and UJSONResponse fast serialisers.