fastapi/fastapi
FastAPI
FastAPI is a Python web framework for building HTTP and WebSocket APIs from typed Python functions. It uses Pydantic for data validation and serialization, Starlette for the underlying ASGI plumbing, and produces an OpenAPI 3.1 schema (with Swagger UI and ReDoc) directly from your function signatures.
This wiki documents the source code in fastapi/ — the framework itself, not the user-facing tutorial site at https://fastapi.tiangolo.com.
What this project is
The repository hosts the fastapi package distributed on PyPI. A FastAPI application is built by:
- Instantiating
FastAPI()(the class lives infastapi/applications.py). - Declaring path operation functions with decorators (
@app.get(...),@app.post(...), etc.). - Annotating function parameters with Python type hints, optional
Annotated[...]metadata, and FastAPI parameter markers (Path,Query,Body,Form,File,Header,Cookie,Depends,Security).
At request time FastAPI walks the function's signature, builds a Dependant tree (fastapi/dependencies/), validates the request against Pydantic models, calls the function (synchronously in a threadpool, or directly if async def), and serializes the return value into a Response.
High-level pieces
| Layer | Files | Role |
|---|---|---|
| Public API | fastapi/__init__.py |
Re-exports the small surface that user code imports (FastAPI, APIRouter, Depends, HTTPException, Request, Response, BackgroundTasks, parameter helpers, etc.). |
| Application | fastapi/applications.py |
The FastAPI class itself — a subclass of Starlette that adds OpenAPI, docs UIs, custom exception handlers, and routing helpers. |
| Routing | fastapi/routing.py |
APIRouter, APIRoute, APIWebSocketRoute, dependency-aware request/response wrappers, response serialization. |
| Dependencies | fastapi/dependencies/ |
The Dependant dataclass and the resolver in utils.py that walks a function signature and produces request-bound values. |
| Parameters | fastapi/params.py, fastapi/param_functions.py |
The Param family of pydantic.FieldInfo subclasses (Path, Query, Header, Cookie, Body, Form, File) and their function helpers. |
| OpenAPI | fastapi/openapi/ |
Pydantic models for the OpenAPI 3.1 spec, the schema generator, Swagger UI/ReDoc HTML templates, OAuth2 redirect HTML. |
| Security | fastapi/security/ |
API key, HTTP (Basic, Bearer, Digest), OAuth2 (password, authorization-code, implicit, client-credentials), OpenID Connect schemes. |
| Middleware | fastapi/middleware/ |
Thin re-exports of Starlette middlewares plus AsyncExitStackMiddleware for dependency-with-yield cleanup. |
| Pydantic compat | fastapi/_compat/ |
A small abstraction over Pydantic v2 (Pydantic v1 was dropped). |
| Encoding | fastapi/encoders.py |
jsonable_encoder() — converts arbitrary Python values (Pydantic models, dataclasses, dates, UUIDs, paths, …) into JSON-compatible primitives. |
| Exceptions | fastapi/exceptions.py, fastapi/exception_handlers.py |
HTTPException, WebSocketException, validation errors, default handlers. |
| Streaming | fastapi/sse.py |
Server-Sent Events helpers (EventSourceResponse, ServerSentEvent). |
Where to start reading
| If you want to understand… | Read |
|---|---|
| How a request becomes a response | Architecture and Request lifecycle. |
How Depends(...) works |
Dependency injection. |
How /docs and /redoc get their schema |
OpenAPI generation. |
Auth schemes (OAuth2PasswordBearer, etc.) |
Security. |
| How the codebase is built and tested | Getting started and How to contribute. |
Repository layout (top level)
fastapi/ # The framework package itself.
fastapi-slim/ # The "slim" distribution — same code, alternate package metadata.
tests/ # Pytest test suite (~580 files, ~75k lines).
docs/ # Multi-language Markdown content for the documentation site.
docs_src/ # Runnable example apps used in the docs and as test fixtures.
scripts/ # Build, lint, format, docs, translation, and release tooling.
.github/ # GitHub Actions workflows + issue/PR templates.
pyproject.toml # Project metadata, optional-dependency groups, ruff/mypy/pytest config.The framework code is small (~19k lines across 48 Python files), but the surrounding docs and tests are large. See By the numbers for measurements.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.
Next
Architecture