fastapi-best-practices
Original:🇺🇸 English
Translated
1 scriptsChecked / no sensitive code detected
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.
1installs
Sourceahmed6ww/ax-agents
Added on
NPX Install
npx skill4agent add ahmed6ww/ax-agents fastapi-best-practicesTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →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)
- - Use
async-io-intensivefor awaitables,async deffor blocking I/Odef - - Offload CPU work to multiprocessing/Celery
async-cpu-intensive - - Use
async-sync-sdkfor 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 serialization
pydantic-custom-base - - Split BaseSettings by domain
pydantic-config - - Raise ValueError for validation errors
pydantic-value-error
4. Dependency Injection (MEDIUM-HIGH)
- - Use Depends for DB-backed validation
dependency-validation - - Chain dependencies for reuse
dependency-chaining - - Decouple into small reusable units
dependency-decoupling - - Prefer async dependencies
dependency-async
5. Database & Storage (MEDIUM)
- - Strict naming (snake_case, singular, etc.)
db-naming-conventions - - Explicit index naming conventions
db-index-naming - - Prefer SQL for complex data logic
db-sql-first - - Static, descriptive, reversible migrations
db-migrations
6. REST & API Design (MEDIUM)
- - Consistent path vars for dependency reuse
api-path-naming - - Minimize serialization overhead
api-response-serialization - - Hide docs in production
api-docs-hiding - - Detailed OpenAPI metadata
api-docs-customization
7. Testing & Tooling (LOW-MEDIUM)
- - Use AsyncClient/httpx for tests
tooling-test-client - - Use Ruff for all linting
tooling-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.mdFull Compiled Document
For the complete guide with all rules expanded:
AGENTS.md