Learn-skills.dev fastapi-best-practices
FastAPI production-grade best practices and guidelines for building scalable, high-performance web APIs. Covers project structure, async concurrency, Pydantic validation, dependency injection, and database patterns.
install
source · Clone the upstream repo
git clone https://github.com/NeverSight/learn-skills.dev
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/NeverSight/learn-skills.dev "$T" && mkdir -p ~/.claude/skills && cp -r "$T/data/skills-md/ahmed6ww/ax-agents/fastapi-best-practices" ~/.claude/skills/neversight-learn-skills-dev-fastapi-best-practices && rm -rf "$T"
manifest:
data/skills-md/ahmed6ww/ax-agents/fastapi-best-practices/SKILL.mdsource content
FastAPI Best Practices
Comprehensive production-grade best practices for FastAPI applications. Contains rules across 7 categories to guide automated refactoring and code generation.
When to Apply
Reference these guidelines when:
- Setting up a new FastAPI project structure
- Implementing async routes and handling blocking I/O
- Designing Pydantic models and validation logic
- Structuring dependencies for reusable validation and injection
- Integrating databases with SQLAlchemy and Alembic
- Writing tests and configuring CI/CD tooling
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Async & Concurrency | CRITICAL | |
| 2 | Project Structure | HIGH | |
| 3 | Pydantic Patterns | HIGH | |
| 4 | Dependency Injection | MEDIUM-HIGH | |
| 5 | Database & Storage | MEDIUM | |
| 6 | REST & API Design | MEDIUM | |
| 7 | Testing & Tooling | LOW-MEDIUM | |
| 8 | Code Maintenance | LOW | |
| 9 | Performance Optimization | MEDIUM | |
| 10 | TDD Strategy | HIGH | |
Quick Reference
1. Async & Concurrency (CRITICAL)
- Useasync-io-intensive
for awaitables,async def
for blocking I/Odef
- Offload CPU work to multiprocessing/Celeryasync-cpu-intensive
- Useasync-sync-sdk
for sync SDKs in async routesrun_in_threadpool
2. Project Structure (HIGH)
- Organize by domain (structure-domain-driven
), not file typesrc/{domain}
3. Pydantic Patterns (HIGH)
- Use built-in regex, enums, etc.pydantic-validation
- Use custom base model for global serializationpydantic-custom-base
- Split BaseSettings by domainpydantic-config
- Raise ValueError for validation errorspydantic-value-error
4. Dependency Injection (MEDIUM-HIGH)
- Use Depends for DB-backed validationdependency-validation
- Chain dependencies for reusedependency-chaining
- Decouple into small reusable unitsdependency-decoupling
- Prefer async dependenciesdependency-async
5. Database & Storage (MEDIUM)
- Strict naming (snake_case, singular, etc.)db-naming-conventions
- Explicit index naming conventionsdb-index-naming
- Prefer SQL for complex data logicdb-sql-first
- Static, descriptive, reversible migrationsdb-migrations
6. REST & API Design (MEDIUM)
- Consistent path vars for dependency reuseapi-path-naming
- Minimize serialization overheadapi-response-serialization
- Hide docs in productionapi-docs-hiding
- Detailed OpenAPI metadataapi-docs-customization
7. Testing & Tooling (LOW-MEDIUM)
- Use AsyncClient/httpx for teststooling-test-client
- Use Ruff for all lintingtooling-linter
8. New Capabilities (Merged)
- Scripts: Helper scripts available in
(e.g.,scripts/
).run_ruff.py - References: Additional guides in
(e.g.,references/
).quad_strategy.md - TDD: Explicit testing strategies in
.rules/tdd-strategy.md
How to Use
Read individual rule files for detailed explanations and code examples:
rules/async-io-intensive.md rules/structure-domain-driven.md
Full Compiled Document
For the complete guide with all rules expanded:
AGENTS.md