FastAPI — codebase report

Built at commit 5758a9c83

FastAPI is a Python web framework for building HTTP and WebSocket APIs from type-annotated functions. The package in fastapi/ builds on Starlette (ASGI routing and middleware) and Pydantic (validation and serialization), and it generates an OpenAPI schema and the Swagger UI and ReDoc pages from your route declarations. The repo also holds a large docs tree in many languages, a test suite bigger than the package, and scripts that build and translate the docs. A newcomer starts with fastapi/applications.py (the FastAPI class), then fastapi/routing.py (APIRouter, APIRoute and the request handler) and fastapi/dependencies/utils.py (dependency resolution). This report has 13 diagrams. It has no data model, sitemap, state, journey or swimlane chart, because the code has no database schema, page tree, status field or multi-role process to draw. History, hot files, module imports and co-change charts are rolled up, as each section says.

The stack

The chart shows the main parts of the fastapi package, the libraries it builds on, and the repo tooling. A path operation function that you write is registered on an APIRouter, which makes one APIRoute per path. Each APIRoute builds a request handler that calls solve_dependencies and uses ModelField (a wrapper around a Pydantic TypeAdapter) to validate and serialize. Start with these boxes, then read the sequence and flowchart diagrams for the detail.

The stackThe stack

Edit in Diagrammo ↗

Size by directory

The treemap shows lines of text by directory. The measurement rolled up 3242 files to 448 boxes, two directory levels deep; docs/en/docs/img/sponsors and tests hold more than 40 files of their own, so they show as files. The smallest 409 boxes are shown as "(other)" (44906 lines). The docs tree dominates: docs/en has 39718 lines and each of 12 translations has about 18400 to 18700. The fastapi package itself has 16249 lines in its own files, and tests/test_tutorial alone has 29871.

Size by directorySize by directory

Edit in Diagrammo ↗

Lines by language

The pie shows lines by language. Markdown has 252126 lines and Python has 132812, so most of the text in the repo is documentation. YAML has 7155 lines, JavaScript 614 and CSS 589. The 3 smallest languages (TOML, HTML and Shell) are grouped as "Other" with 583 lines.

Lines by languageLines by language

Edit in Diagrammo ↗

Releases and largest commits

The chart shows the 5 largest commits by lines changed and the newest 15 release tags. The repo has 8045 commits from 2018-12-05 to 2026-11-13 and 354 tags, but only the newest 15 tags are shown. Events are evenly spaced, not to scale, because the 15 tags fall in 24 days (2026-10-21 to 2026-11-13) while the 5 largest commits span 2020 to 2026. The largest commit changed 68216 lines in 280 files (the OpenAPI test refactor of 2023-05-08).

Releases and largest commitsReleases and largest commits

Edit in Diagrammo ↗

Registering a path operation

The chart traces what happens when you write @app.get on a function. FastAPI.get hands the call to APIRouter, which merges prefix, tags and dependencies and then builds an APIRoute. APIRoute builds the dependant (the object that describes the function's parameters), the response field and the request handler. The router then appends the route and bumps a version counter, so app.openapi() sees the new route.

Registering a path operationRegistering a path operation

Edit in Diagrammo ↗

Handling one HTTP request

The chart traces one HTTP request from the ASGI server to the response. On the first call FastAPI builds its middleware stack, and the request passes through it to the router, which finds the matching APIRoute. The request handler reads the body, calls solve_dependencies, and then either runs your function and serializes the result or raises RequestValidationError. This path is the core of the framework; read it before the dependency and body flowcharts.

Handling one HTTP requestHandling one HTTP request

Edit in Diagrammo ↗

A protected route rejects a request

The chart shows a request without an Authorization header reaching a route that depends on OAuth2PasswordBearer. solve_dependencies awaits the security object, and the object raises an HTTPException with status 401 and a WWW-Authenticate: Bearer header. ExceptionMiddleware turns that exception into a JSON response. If the header is a valid bearer token, the token is passed to your function instead.

A protected route rejects a requestA protected route rejects a request

Edit in Diagrammo ↗

Building the OpenAPI schema and docs pages

The chart shows how FastAPI serves /docs and /openapi.json. The /docs page is plain HTML that loads the Swagger UI bundle from a CDN. The /openapi.json handler calls get_openapi, which collects the request and response models from all routes and builds one path item per route. The app caches the result and rebuilds it only when the router's routes version changes.

Building the OpenAPI schema and docs pagesBuilding the OpenAPI schema and docs pages

Edit in Diagrammo ↗

How solve_dependencies resolves one dependant

The flowchart shows the loop inside solve_dependencies. For each sub-dependant it solves the sub-dependant first (recursively), then reuses a cached value, enters a generator on an exit stack, awaits a coroutine, or runs a plain function in a thread pool. After the loop it validates path, query, header, cookie and body parameters, and injects Request, Response, BackgroundTasks and SecurityScopes. The function returns the values and any errors; the caller decides what to do with the errors.

How solve_dependencies resolves one dependantHow solve_dependencies resolves one dependant

Edit in Diagrammo ↗

How the request handler reads the body

The flowchart shows how the request handler decides what the request body is. A form body is read with request.form. Other bodies are read as bytes and parsed as JSON only when the Content-Type is application/json or a +json type, or when it is missing and strict content type is off. Invalid JSON becomes a RequestValidationError of type json_invalid; any other bytes stay raw.

How the request handler reads the bodyHow the request handler reads the body

Edit in Diagrammo ↗

Hot files: size against edits

The bubble chart shows the 40 most edited files of the 2853 files edited in the last 12 months (to 2026-11-13). Each bubble sits at lines in the file and number of commits, and its size is the file's bytes. docs/en/docs/release-notes.md is far ahead with 1010 edits and 7912 lines. In the code, fastapi/__init__.py has 125 edits, fastapi/dependencies/utils.py has 66, and fastapi/routing.py has 48 edits and 6552 lines.

Hot files: size against editsHot files: size against edits

Edit in Diagrammo ↗

Imports between directories

The arc chart shows imports between directories. The measurement rolled up 1812 file imports to 157 links between 95 directories, up to 2 levels deep; the 56 least connected directories are shown as "(other)". The strongest link is tests to fastapi with 479 imports, then tests/test_tutorial to fastapi with 256. Inside the package, fastapi/openapi imports the top-level fastapi modules 11 times, and fastapi/security imports fastapi/openapi 5 times.

Imports between directoriesImports between directories

Edit in Diagrammo ↗

Files that change together

The arc chart shows the 30 most frequent of 32124 file pairs that change in the same commit; 105 bulk commits were skipped. docs/en/docs/release-notes.md and fastapi/__init__.py change together 266 times, which is the release step. README.md and docs/en/data/sponsors.yml change together 101 times. In the code, fastapi/applications.py and fastapi/routing.py change together 60 times, and fastapi/dependencies/utils.py and fastapi/routing.py 50 times.

Files that change togetherFiles that change together

Edit in Diagrammo ↗