FastAPI best practices and conventions. Use when writing, reviewing, or refactoring FastAPI applications — route handlers, Pydantic schemas, dependency injection, project structure, async patterns, testing, or API documentation. Triggers on tasks involving FastAPI routers, endpoints, request validation, response models, or application configuration. Does not cover general Python syntax or typing — see modern-python-development for that.
72
90%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
View guide
Passed
No findings from the security scan
Opinionated conventions for building production FastAPI applications. General Python idioms (naming, type hints, error handling, dataclasses) are covered by modern-python-development — this skill focuses on FastAPI-specific patterns.
| Category | Impact | Reference |
|---|---|---|
| Project Structure | HIGH | references/project-conventions.md |
| Async Routes | CRITICAL | references/async-patterns.md |
| Pydantic Integration | HIGH | references/pydantic-patterns.md |
| Dependency Injection | HIGH | references/dependencies.md |
| Database & Migrations | MEDIUM | references/project-conventions.md |
| Testing | MEDIUM | references/project-conventions.md |
| API Documentation | LOW | references/project-conventions.md |
async def — use ONLY with non-blocking await calls; blocks event loop otherwisedef (sync) — use for blocking I/O; runs in threadpool automaticallyrun_in_threadpool() from StarletteSee references/async-patterns.md for decision matrix, threadpool caveats, and examples.
Organize by domain, not by file type:
src/
├── auth/ # Domain package
│ ├── router.py # Endpoints
│ ├── schemas.py # Pydantic models
│ ├── models.py # DB models
│ ├── service.py # Business logic
│ ├── dependencies.py # Route dependencies
│ ├── config.py # Env vars (BaseSettings)
│ ├── constants.py # Constants, error codes
│ ├── exceptions.py # Domain exceptions
│ └── utils.py # Helpers
├── posts/ # Another domain
│ └── ...
├── config.py # Global config
├── database.py # DB connection
└── main.py # App initfrom src.auth import constants as auth_constantsSee references/project-conventions.md for full layout, DB naming, Alembic, and linting.
Field, EmailStr, AnyUrl) before writing custom onesBaseSettings by domain — one per module, not a single global configValueError in validators becomes a 422 response with the full messageSee references/pydantic-patterns.md for base model template, schema design, and ORM mode.
async dependencies to avoid threadpool overhead on trivial operationsSee references/dependencies.md for chaining, auth, pagination, and DB session patterns.
lower_case_snake, singular (post, user, post_like)_at suffix; date columns: _date suffixSee references/project-conventions.md for index naming template, Alembic migration conventions, and SQL-first examples.
See references/project-conventions.md for async test fixture setup.
openapi_url=None for non-allowed environmentsresponse_model, status_code, description, tags on endpointsSee references/project-conventions.md for docs configuration and endpoint documentation examples.
Each reference file contains detailed explanations, correct/incorrect code examples, and rationale. Read individual files as needed for the category you're working on.
298fce7
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.