fastapi-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFastAPI Best Practices
FastAPI 最佳实践
Comprehensive production-grade best practices for FastAPI applications. Contains rules across 7 categories to guide automated refactoring and code generation.
针对FastAPI应用的全面生产级最佳实践。包含多类规则,可指导自动化重构与代码生成。
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
以下场景可参考本指南:
- 搭建新的FastAPI项目结构时
- 实现异步路由并处理阻塞式I/O时
- 设计Pydantic模型与验证逻辑时
- 构建可复用验证与注入的依赖结构时
- 通过SQLAlchemy和Alembic集成数据库时
- 编写测试并配置CI/CD工具时
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 | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 异步与并发 | 关键 | |
| 2 | 项目结构 | 高 | |
| 3 | Pydantic 模式 | 高 | |
| 4 | 依赖注入 | 中高 | |
| 5 | 数据库与存储 | 中 | |
| 6 | REST 与 API 设计 | 中 | |
| 7 | 测试与工具 | 中低 | |
| 8 | 代码维护 | 低 | |
| 9 | 性能优化 | 中 | |
| 10 | TDD 策略 | 高 | |
Quick Reference
快速参考
1. Async & Concurrency (CRITICAL)
1. 异步与并发(关键)
- - 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
- - 对可等待对象使用
async-io-intensive,对阻塞式I/O使用async defdef - - 将CPU密集型工作卸载到多进程或Celery
async-cpu-intensive - - 在异步路由中使用
async-sync-sdk处理同步SDKrun_in_threadpool
2. Project Structure (HIGH)
2. 项目结构(高)
- - Organize by domain (
structure-domain-driven), not file typesrc/{domain}
- - 按领域(
structure-domain-driven)而非文件类型组织代码src/{domain}
3. Pydantic Patterns (HIGH)
3. Pydantic 模式(高)
- - 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
- - 使用内置的正则表达式、枚举等
pydantic-validation - - 使用自定义基础模型实现全局序列化
pydantic-custom-base - - 按领域拆分BaseSettings
pydantic-config - - 验证错误时抛出ValueError
pydantic-value-error
4. Dependency Injection (MEDIUM-HIGH)
4. 依赖注入(中高)
- - 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
- - 使用Depends实现基于数据库的验证
dependency-validation - - 链式调用依赖以实现复用
dependency-chaining - - 拆分为小型可复用单元
dependency-decoupling - - 优先使用异步依赖
dependency-async
5. Database & Storage (MEDIUM)
5. 数据库与存储(中)
- - 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
- - 严格遵循命名规范(蛇形命名、单数形式等)
db-naming-conventions - - 明确的索引命名规范
db-index-naming - - 复杂数据逻辑优先使用SQL
db-sql-first - - 静态、可描述、可回滚的迁移
db-migrations
6. REST & API Design (MEDIUM)
6. REST 与 API 设计(中)
- - 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
- - 统一路径变量以实现依赖复用
api-path-naming - - 最小化序列化开销
api-response-serialization - - 生产环境中隐藏文档
api-docs-hiding - - 详细的OpenAPI元数据
api-docs-customization
7. Testing & Tooling (LOW-MEDIUM)
7. 测试与工具(中低)
- - Use AsyncClient/httpx for tests
tooling-test-client - - Use Ruff for all linting
tooling-linter
- - 使用AsyncClient/httpx进行测试
tooling-test-client - - 使用Ruff进行所有代码检查
tooling-linter
8. New Capabilities (Merged)
8. 新功能(已合并)
- 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
- 脚本:目录下提供辅助脚本(如
scripts/)。run_ruff.py - 参考资料:目录下提供额外指南(如
references/)。quad_strategy.md - TDD:中包含明确的测试策略。
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阅读单个规则文件以获取详细说明和代码示例:
rules/async-io-intensive.md
rules/structure-domain-driven.mdFull Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需查看包含所有扩展规则的完整指南:
AGENTS.md