# fastapi/fastapi architecture

> Architecture diagram of the GitHub repository fastapi/fastapi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fastapi/fastapi
- Repository: https://github.com/fastapi/fastapi
- Video tour (about a minute): https://gitdiagram.com/fastapi/fastapi/video

## Overview

FastAPI is a Python ASGI framework for declaring typed API routes and serving validated requests with serialized responses. The main runtime path runs from an HTTP client through the application and router, into dependency resolution and parameter validation, then to user endpoint code and response encoding. Separate branches provide security dependencies, OpenAPI and interactive docs, middleware, WebSockets/SSE, and supporting response features. The graph follows the supplied tree, README, and sampled excerpts; unsampled subsystem links are omitted where their call wiring is not established.

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Request runtime"]
  node_app["FastAPI app<br/>[applications.py]"]
  node_middleware["Middleware stack<br/>[applications.py]"]
  node_router["Route dispatch<br/>[routing.py]"]
  node_deps["Dependency solving<br/>[utils.py]"]
  node_encoding["Response encoding<br/>[encoders.py]"]
  node_responses["HTTP responses<br/>[responses.py]"]
  node_errors["Exception handling"]
end

subgraph group_contracts["Types and security"]
  node_validation["Model compatibility<br/>[v2.py]"]
  node_param_types["Parameter declarations<br/>[params.py]"]
  node_security["Security schemes"]
end

subgraph group_docs["API documentation"]
  node_openapi["OpenAPI generation<br/>[utils.py]"]
  node_docs_ui["Interactive docs<br/>[docs.py]"]
end

subgraph group_extensions["Protocols and extensions"]
  node_middleware_modules["Built-in middleware"]
  node_websocket["WebSocket support<br/>[websockets.py]"]
  node_sse["Server events<br/>[sse.py]"]
  node_background["Background tasks<br/>[background.py]"]
  node_static["Static files<br/>[staticfiles.py]"]
  node_templates["Template responses<br/>[templating.py]"]
end

node_client(("HTTP client"))
node_endpoint["User endpoint"]

node_client -->|"sends request"| node_app
node_app -->|"builds stack"| node_middleware
node_middleware -->|"dispatches"| node_router
node_router -->|"resolves inputs"| node_deps
node_deps -->|"interprets declarations"| node_param_types
node_deps -->|"validates fields"| node_validation
node_deps -->|"resolves schemes"| node_security
node_router -->|"invokes endpoint"| node_endpoint
node_endpoint -->|"returns value"| node_router
node_router -->|"encodes result"| node_encoding
node_router -->|"creates response"| node_responses
node_router -->|"handles failures"| node_errors
node_app -->|"generates schema"| node_openapi
node_openapi -->|"builds schemas"| node_validation
node_app -->|"serves documentation"| node_docs_ui
node_docs_ui -->|"loads schema"| node_openapi
node_app -->|"configures middleware"| node_middleware_modules
node_router -->|"dispatches sockets"| node_websocket
node_router -->|"supports event streams"| node_sse
node_router -->|"schedules tasks"| node_background
node_router -->|"serves static assets"| node_static
node_router -->|"renders templates"| node_templates

click node_app "https://github.com/fastapi/fastapi/blob/master/fastapi/applications.py"
click node_middleware "https://github.com/fastapi/fastapi/blob/master/fastapi/applications.py"
click node_router "https://github.com/fastapi/fastapi/blob/master/fastapi/routing.py"
click node_deps "https://github.com/fastapi/fastapi/blob/master/fastapi/dependencies/utils.py"
click node_encoding "https://github.com/fastapi/fastapi/blob/master/fastapi/encoders.py"
click node_responses "https://github.com/fastapi/fastapi/blob/master/fastapi/responses.py"
click node_errors "https://github.com/fastapi/fastapi/blob/master/fastapi/exception_handlers.py"
click node_validation "https://github.com/fastapi/fastapi/blob/master/fastapi/_compat/v2.py"
click node_param_types "https://github.com/fastapi/fastapi/blob/master/fastapi/params.py"
click node_security "https://github.com/fastapi/fastapi/tree/master/fastapi/security"
click node_openapi "https://github.com/fastapi/fastapi/blob/master/fastapi/openapi/utils.py"
click node_docs_ui "https://github.com/fastapi/fastapi/blob/master/fastapi/openapi/docs.py"
click node_middleware_modules "https://github.com/fastapi/fastapi/tree/master/fastapi/middleware"
click node_websocket "https://github.com/fastapi/fastapi/blob/master/fastapi/websockets.py"
click node_sse "https://github.com/fastapi/fastapi/blob/master/fastapi/sse.py"
click node_background "https://github.com/fastapi/fastapi/blob/master/fastapi/background.py"
click node_static "https://github.com/fastapi/fastapi/blob/master/fastapi/staticfiles.py"
click node_templates "https://github.com/fastapi/fastapi/blob/master/fastapi/templating.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app,node_middleware,node_router,node_deps,node_encoding,node_responses,node_errors,node_client,node_endpoint toneBlue
class node_validation,node_param_types,node_security toneAmber
class node_openapi,node_docs_ui toneMint
class node_middleware_modules,node_websocket,node_sse,node_background,node_static,node_templates toneRose
```

## Components

### Request runtime

- [FastAPI app](https://github.com/fastapi/fastapi/blob/master/fastapi/applications.py): `fastapi/applications.py`
- [Middleware stack](https://github.com/fastapi/fastapi/blob/master/fastapi/applications.py): `fastapi/applications.py`
- [Route dispatch](https://github.com/fastapi/fastapi/blob/master/fastapi/routing.py): `fastapi/routing.py`
- [Dependency solving](https://github.com/fastapi/fastapi/blob/master/fastapi/dependencies/utils.py): `fastapi/dependencies/utils.py`
- [Response encoding](https://github.com/fastapi/fastapi/blob/master/fastapi/encoders.py): `fastapi/encoders.py`
- [HTTP responses](https://github.com/fastapi/fastapi/blob/master/fastapi/responses.py): `fastapi/responses.py`
- [Exception handling](https://github.com/fastapi/fastapi/blob/master/fastapi/exception_handlers.py): `fastapi/exception_handlers.py`

### Types and security

- [Model compatibility](https://github.com/fastapi/fastapi/blob/master/fastapi/_compat/v2.py): `fastapi/_compat/v2.py`
- [Parameter declarations](https://github.com/fastapi/fastapi/blob/master/fastapi/params.py): `fastapi/params.py`
- [Security schemes](https://github.com/fastapi/fastapi/tree/master/fastapi/security): `fastapi/security`

### API documentation

- [OpenAPI generation](https://github.com/fastapi/fastapi/blob/master/fastapi/openapi/utils.py): `fastapi/openapi/utils.py`
- [Interactive docs](https://github.com/fastapi/fastapi/blob/master/fastapi/openapi/docs.py): `fastapi/openapi/docs.py`

### Protocols and extensions

- [Built-in middleware](https://github.com/fastapi/fastapi/tree/master/fastapi/middleware): `fastapi/middleware`
- [WebSocket support](https://github.com/fastapi/fastapi/blob/master/fastapi/websockets.py): `fastapi/websockets.py`
- [Server events](https://github.com/fastapi/fastapi/blob/master/fastapi/sse.py): `fastapi/sse.py`
- [Background tasks](https://github.com/fastapi/fastapi/blob/master/fastapi/background.py): `fastapi/background.py`
- [Static files](https://github.com/fastapi/fastapi/blob/master/fastapi/staticfiles.py): `fastapi/staticfiles.py`
- [Template responses](https://github.com/fastapi/fastapi/blob/master/fastapi/templating.py): `fastapi/templating.py`

### Other components

- **HTTP client**
- **User endpoint**

## Connections

- HTTP client → FastAPI app: sends request
- FastAPI app → Middleware stack: builds stack
- Middleware stack → Route dispatch: dispatches
- Route dispatch → Dependency solving: resolves inputs
- Dependency solving → Parameter declarations: interprets declarations
- Dependency solving → Model compatibility: validates fields
- Dependency solving → Security schemes: resolves schemes
- Route dispatch → User endpoint: invokes endpoint
- User endpoint → Route dispatch: returns value
- Route dispatch → Response encoding: encodes result
- Route dispatch → HTTP responses: creates response
- Route dispatch → Exception handling: handles failures
- FastAPI app → OpenAPI generation: generates schema
- OpenAPI generation → Model compatibility: builds schemas
- FastAPI app → Interactive docs: serves documentation
- Interactive docs → OpenAPI generation: loads schema
- FastAPI app → Built-in middleware: configures middleware
- Route dispatch → WebSocket support: dispatches sockets
- Route dispatch → Server events: supports event streams
- Route dispatch → Background tasks: schedules tasks
- Route dispatch → Static files: serves static assets
- Route dispatch → Template responses: renders templates
