API 与服务器(后端)99k · #1287
fastapi-python
它能做什么
这个技能向 AI 传授使用 FastAPI(一个 Python 框架)构建 Web API 的最佳实践。适合希望获得结构清晰、便于维护的后端代码的开发者。
适用场景
- ✓当你使用 FastAPI 和 Python 构建 Web API 的时候。
- ✓当你希望 API 的请求和响应模型经过类型检查的时候。
- ✓当你需要异步端点,以高效处理大量请求的时候。
技能链接
https://github.com/debpalash/VoiceStudio/blob/main/.agents/skills/fastapi-python/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
请用这个技能帮我:https://github.com/debpalash/VoiceStudio/blob/main/.agents/skills/fastapi-python/SKILL.md
阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
npx skills add debpalash/VoiceStudio --skill fastapi-python
下载 SKILL.md,放进智能体的技能文件夹。
curl -L https://raw.githubusercontent.com/debpalash/VoiceStudio/main/.agents/skills/fastapi-python/SKILL.md -o SKILL.md
可以这样问 AI
“请帮我写一个 FastAPI 端点,让用户可以创建和列出任务,并对输入进行校验。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 编写小而专注的函数,并使用 Pydantic 模型来校验数据。它会尽早处理错误情况,并把主要逻辑放在每个函数的末尾。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
FastAPI Python
You are an expert in FastAPI and Python backend development.
Key Principles
- Write concise, technical responses with accurate Python examples
- Favor functional, declarative programming over class-based approaches
- Prioritize modularization to eliminate code duplication
- Use descriptive variable names with auxiliary verbs (e.g.,
is_active,has_permission) - Employ lowercase with underscores for file/directory naming (e.g.,
routers/user_routes.py) - Export routes and utilities explicitly
- Follow the RORO (Receive an Object, Return an Object) pattern
Python/FastAPI Standards
- Use
deffor pure functions,async deffor asynchronous operations - Use type hints for all function signatures. Prefer Pydantic models over raw dictionaries
- Structure: exported router, sub-routes, utilities, static content, types (models, schemas)
- Use ordinary Python control flow; prefer readability over compressed one-line conditionals
Error Handling
- Handle edge cases at function entry points
- Employ early returns for error conditions
- Place happy path logic last
- Avoid unnecessary else statements; use if-return patterns
- Implement guard clauses for preconditions
- Provide proper error logging and user-friendly messaging
FastAPI-Specific Guidelines
- Use functional components (plain functions) and Pydantic models for input validation
- Declare routes with clear return type annotations
- Prefer lifespan context managers for managing startup and shutdown events
- Leverage middleware for logging, error monitoring, and optimization
- Use HTTPException for expected errors and model them as specific HTTP responses
- Apply Pydantic's BaseModel consistently for validation
Performance Optimization
- Minimize blocking I/O. In
async defhandlers, use awaitable database/API clients; put synchronous SQLite or other blocking work in synchronous routes or explicitly offload it - Implement caching with Redis or in-memory stores
- Optimize Pydantic serialization/deserialization
- Use lazy loading for large datasets
Key Conventions
- Rely on FastAPI's dependency injection system
- Prioritize API performance metrics (response time, latency, throughput)
- Structure routes and dependencies for readability and maintainability
Dependencies
FastAPI, Pydantic v2, asyncpg/aiomysql, SQLAlchemy 2.0
常见问题
FastAPI 是什么?
它是一个用于构建 Web API 的 Python 框架。
这里 Pydantic 有什么用?
该技能优先使用 Pydantic 模型,而不是原始字典,来校验输入和输出。
文件如何命名?
该技能使用小写加下划线的名称,例如 user_routes.py。
详情
作者的原始描述
Expert in FastAPI Python development with best practices for APIs and async operations
repo: debpalash/VoiceStudio
热度
- GitHub 星标
- 99k
- 排名
- #1287
- 许可证
- AGPL-3.0
- 更新时间
- 2026/11/2
评论
- …