# tiangolo/fastapi architecture

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

- Interactive diagram: https://gitdiagram.com/tiangolo/fastapi
- Repository: https://github.com/tiangolo/fastapi

## Overview

FastAPI is a Python web framework for building typed APIs on Starlette and Pydantic. Developers declare applications, routes, parameters, dependencies, and security schemes; incoming ASGI requests are routed, dependency values and request data are resolved and validated, endpoint code runs, and responses are serialized. Separate capabilities include WebSockets, streaming/SSE, generated OpenAPI and interactive docs, middleware, background tasks, and telemetry. The map follows the supplied excerpts and SOURCE INDEX references; some internals and optional integrations were not sampled, so uncertain links are omitted.

Main entry points: [`fastapi/applications.py`](https://github.com/tiangolo/fastapi/blob/master/fastapi/applications.py) (FastAPI framework), [`fastapi/routing.py`](https://github.com/tiangolo/fastapi/blob/master/fastapi/routing.py) (Route registry), [`fastapi/dependencies/utils.py`](https://github.com/tiangolo/fastapi/blob/master/fastapi/dependencies/utils.py) (Dependency resolver), [`fastapi/openapi/utils.py`](https://github.com/tiangolo/fastapi/blob/master/fastapi/openapi/utils.py) (OpenAPI generation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["Application and routing"]
  node_framework["FastAPI framework<br/>[applications.py]"]
  node_router["Route registry<br/>[routing.py]"]
  node_endpoint["Endpoint handler"]
end

subgraph group_request["Request processing"]
  node_params["Parameter declarations<br/>[params.py]"]
  node_dependency["Dependency resolver<br/>[utils.py]"]
  node_validation["Model validation<br/>[v2.py]"]
  node_security["Security schemes<br/>[oauth2.py]"]
  node_errors["Request errors<br/>[exceptions.py]"]
end

subgraph group_output["Responses and documentation"]
  node_response["Response serialization<br/>[responses.py]"]
  node_encoder["Data encoding<br/>[encoders.py]"]
  node_openapi["OpenAPI generation<br/>[utils.py]"]
  node_docs["Interactive docs<br/>[docs.py]"]
end

subgraph group_extensions["Framework capabilities"]
  node_middleware["Middleware stack<br/>[asyncexitstack.py]"]
  node_background["Background tasks<br/>[background.py]"]
  node_websocket["WebSocket routes<br/>[routing.py]"]
  node_sse["Server-sent events<br/>[sse.py]"]
  node_telemetry["Telemetry<br/>[_api.py]"]
end

node_developer(("API developer"))
node_client(("HTTP client"))
node_asgi(("ASGI server"))
node_pydantic["Pydantic"]
node_starlette["Starlette"]

node_developer -->|"configures app"| node_framework
node_framework -->|"delegates routes"| node_router
node_asgi -.->|"serves requests"| node_framework
node_client -.->|"sends requests"| node_framework
node_framework -->|"builds stack"| node_middleware
node_framework -->|"generates schema"| node_openapi
node_router -.->|"dispatches route"| node_endpoint
node_router -->|"solves dependencies"| node_dependency
node_router -.->|"builds response"| node_response
node_developer -->|"declares inputs"| node_params
node_dependency -->|"interprets fields"| node_params
node_dependency -->|"validates values"| node_validation
node_dependency -->|"resolves schemes"| node_security
node_dependency -->|"raises validation errors"| node_errors
node_response -->|"encodes content"| node_encoder
node_openapi -->|"reads declarations"| node_params
node_openapi -.->|"documents schemes"| node_security
node_openapi -.->|"provides schema"| node_docs
node_router -.->|"registers routes"| node_websocket
node_router -->|"supports streaming"| node_sse
node_framework -->|"exposes tasks"| node_background
node_framework -->|"configures telemetry"| node_telemetry
node_framework -->|"extends web runtime"| node_starlette
node_validation -->|"uses validation"| node_pydantic

click node_framework "https://github.com/tiangolo/fastapi/blob/master/fastapi/applications.py"
click node_router "https://github.com/tiangolo/fastapi/blob/master/fastapi/routing.py"
click node_params "https://github.com/tiangolo/fastapi/blob/master/fastapi/params.py"
click node_dependency "https://github.com/tiangolo/fastapi/blob/master/fastapi/dependencies/utils.py"
click node_validation "https://github.com/tiangolo/fastapi/blob/master/fastapi/_compat/v2.py"
click node_security "https://github.com/tiangolo/fastapi/blob/master/fastapi/security/oauth2.py"
click node_errors "https://github.com/tiangolo/fastapi/blob/master/fastapi/exceptions.py"
click node_response "https://github.com/tiangolo/fastapi/blob/master/fastapi/responses.py"
click node_encoder "https://github.com/tiangolo/fastapi/blob/master/fastapi/encoders.py"
click node_openapi "https://github.com/tiangolo/fastapi/blob/master/fastapi/openapi/utils.py"
click node_docs "https://github.com/tiangolo/fastapi/blob/master/fastapi/openapi/docs.py"
click node_middleware "https://github.com/tiangolo/fastapi/blob/master/fastapi/middleware/asyncexitstack.py"
click node_background "https://github.com/tiangolo/fastapi/blob/master/fastapi/background.py"
click node_websocket "https://github.com/tiangolo/fastapi/blob/master/fastapi/routing.py"
click node_sse "https://github.com/tiangolo/fastapi/blob/master/fastapi/sse.py"
click node_telemetry "https://github.com/tiangolo/fastapi/blob/master/fastapi/telemetry/_api.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_framework,node_router,node_endpoint,node_client toneBlue
class node_params,node_dependency,node_validation,node_security,node_errors toneAmber
class node_response,node_encoder,node_openapi,node_docs,node_developer,node_asgi toneMint
class node_middleware,node_background,node_websocket,node_sse,node_telemetry toneRose
class node_pydantic,node_starlette toneIndigo
```

## Components

### Application and routing

- [FastAPI framework](https://github.com/tiangolo/fastapi/blob/master/fastapi/applications.py): `fastapi/applications.py`
- [Route registry](https://github.com/tiangolo/fastapi/blob/master/fastapi/routing.py): `fastapi/routing.py`
- **Endpoint handler**

### Request processing

- [Parameter declarations](https://github.com/tiangolo/fastapi/blob/master/fastapi/params.py): `fastapi/params.py`
- [Dependency resolver](https://github.com/tiangolo/fastapi/blob/master/fastapi/dependencies/utils.py): `fastapi/dependencies/utils.py`
- [Model validation](https://github.com/tiangolo/fastapi/blob/master/fastapi/_compat/v2.py): `fastapi/_compat/v2.py`
- [Security schemes](https://github.com/tiangolo/fastapi/blob/master/fastapi/security/oauth2.py): `fastapi/security/oauth2.py`
- [Request errors](https://github.com/tiangolo/fastapi/blob/master/fastapi/exceptions.py): `fastapi/exceptions.py`

### Responses and documentation

- [Response serialization](https://github.com/tiangolo/fastapi/blob/master/fastapi/responses.py): `fastapi/responses.py`
- [Data encoding](https://github.com/tiangolo/fastapi/blob/master/fastapi/encoders.py): `fastapi/encoders.py`
- [OpenAPI generation](https://github.com/tiangolo/fastapi/blob/master/fastapi/openapi/utils.py): `fastapi/openapi/utils.py`
- [Interactive docs](https://github.com/tiangolo/fastapi/blob/master/fastapi/openapi/docs.py): `fastapi/openapi/docs.py`

### Framework capabilities

- [Middleware stack](https://github.com/tiangolo/fastapi/blob/master/fastapi/middleware/asyncexitstack.py): `fastapi/middleware/asyncexitstack.py`
- [Background tasks](https://github.com/tiangolo/fastapi/blob/master/fastapi/background.py): `fastapi/background.py`
- [WebSocket routes](https://github.com/tiangolo/fastapi/blob/master/fastapi/routing.py): `fastapi/routing.py`
- [Server-sent events](https://github.com/tiangolo/fastapi/blob/master/fastapi/sse.py): `fastapi/sse.py`
- [Telemetry](https://github.com/tiangolo/fastapi/blob/master/fastapi/telemetry/_api.py): `fastapi/telemetry/_api.py`

### Other components

- **API developer**
- **HTTP client**
- **ASGI server**
- **Pydantic**
- **Starlette**

## Connections

- API developer → FastAPI framework: configures app (evidence: `fastapi/applications.py`)
- FastAPI framework → Route registry: delegates routes (evidence: `fastapi/applications.py`)
- ASGI server → FastAPI framework: serves requests
- HTTP client → FastAPI framework: sends requests
- FastAPI framework → Middleware stack: builds stack (evidence: `fastapi/applications.py`)
- FastAPI framework → OpenAPI generation: generates schema (evidence: `fastapi/applications.py`)
- Route registry → Endpoint handler: dispatches route
- Route registry → Dependency resolver: solves dependencies (evidence: `fastapi/routing.py`)
- Route registry → Response serialization: builds response
- API developer → Parameter declarations: declares inputs (evidence: `fastapi/params.py`)
- Dependency resolver → Parameter declarations: interprets fields (evidence: `fastapi/dependencies/utils.py`)
- Dependency resolver → Model validation: validates values (evidence: `fastapi/dependencies/utils.py`)
- Dependency resolver → Security schemes: resolves schemes (evidence: `fastapi/dependencies/utils.py`)
- Dependency resolver → Request errors: raises validation errors (evidence: `fastapi/dependencies/utils.py`)
- Response serialization → Data encoding: encodes content (evidence: `fastapi/responses.py`)
- OpenAPI generation → Parameter declarations: reads declarations (evidence: `fastapi/openapi/utils.py`)
- OpenAPI generation → Security schemes: documents schemes
- OpenAPI generation → Interactive docs: provides schema
- Route registry → WebSocket routes: registers routes (evidence: `fastapi/routing.py`)
- Route registry → Server-sent events: supports streaming (evidence: `fastapi/routing.py`)
- FastAPI framework → Background tasks: exposes tasks (evidence: `fastapi/__init__.py`)
- FastAPI framework → Telemetry: configures telemetry (evidence: `fastapi/applications.py`)
- FastAPI framework → Starlette: extends web runtime (evidence: `README.md`)
- Model validation → Pydantic: uses validation (evidence: `README.md`)
