Skip to main content

Overview

FastAPI provides several response classes for different content types. By default, FastAPI returns responses as JSON, but you can customize this behavior using different response classes.

Available Response Classes

FastAPI (via Starlette) provides these response classes:
  • JSONResponse - JSON responses (default)
  • HTMLResponse - HTML content
  • PlainTextResponse - Plain text
  • RedirectResponse - HTTP redirects
  • StreamingResponse - Streaming responses
  • FileResponse - File downloads
  • Response - Generic response class

JSONResponse

The default response class for most FastAPI endpoints:
You typically don’t need to use JSONResponse explicitly - FastAPI uses it by default when you return a dict, list, or Pydantic model.

HTMLResponse

Return HTML content from your endpoints:

Direct HTMLResponse

You can also return an HTMLResponse object directly:
When using response_class=HTMLResponse, you can return the HTML as a string directly. When returning HTMLResponse objects, you have more control over status codes and headers.

PlainTextResponse

Return plain text content:
Or directly:

RedirectResponse

Redirect to another URL:

Redirect Status Codes

Control the redirect type with status codes:
  • Use 307 (Temporary Redirect) to preserve the request method
  • Use 308 (Permanent Redirect) for permanent redirects that preserve the method
  • Use 302 (Found) for temporary redirects that may change the method to GET
  • Use 301 (Moved Permanently) for permanent redirects that may change the method

StreamingResponse

Stream large files or generated content:

Streaming Files

Stream file content:
For static files, consider using FileResponse instead, as it’s optimized for serving files efficiently.

FileResponse

Serve files efficiently with proper headers:

File Download with Custom Headers

Setting Default Response Class

Set a default response class for your entire app or router:
Or for a router:

Custom Response Classes

Create your own response class:
Custom response classes are useful for supporting additional content types like XML, YAML, MessagePack, or proprietary formats.

ORJSONResponse (Deprecated)

ORJSONResponse and UJSONResponse are now deprecated. FastAPI serializes data directly to JSON bytes via Pydantic when a return type or response model is set, which is faster and doesn’t need a custom response class.
If you still need orjson for specific use cases:

Response Class vs Response Model

Understand the difference:
  • response_model: Defines the data structure/schema for validation and documentation
  • response_class: Defines how the response is formatted and sent (HTML, JSON, etc.)

Best Practices

  1. Use the right class: Choose the response class that matches your content type
  2. Set at the decorator: Use response_class parameter in the path decorator for clarity
  3. Default wisely: Set default response classes at the app or router level when appropriate
  4. Stream large data: Use StreamingResponse for large files or generated content
  5. Serve static files properly: Use FileResponse for static files, not StreamingResponse
  6. Document custom types: When using custom response classes, document them properly
  7. Consider performance: Modern FastAPI with Pydantic v2 is very fast - custom JSON serializers are rarely needed