py-fastapi-patterns
FastAPI patterns for API design. Use when creating endpoints, handling dependencies, error handling, or working with OpenAPI schemas.
What this skill does
# FastAPI Patterns
## Problem Statement
FastAPI API design directly affects frontend. Bad patterns here cause frontend bugs, poor developer experience, and integration issues. The OpenAPI schema drives frontend code generation.
---
## Pattern: Dependency Injection
**Problem:** Repetitive code for auth, sessions, and services.
```python
from fastapi import Depends
from sqlalchemy.ext.asyncio import AsyncSession
# ✅ CORRECT: Dependencies for common needs
async def get_session() -> AsyncGenerator[AsyncSession, None]:
async with async_session() as session:
yield session
async def get_current_user(
token: str = Depends(oauth2_scheme),
session: AsyncSession = Depends(get_session),
) -> User:
user = await verify_token_and_get_user(token, session)
if not user:
raise HTTPException(401, "Invalid authentication")
return user
async def get_current_active_user(
user: User = Depends(get_current_user),
) -> User:
if not user.is_active:
raise HTTPException(403, "User is inactive")
return user
# ✅ CORRECT: Endpoint using dependencies
@router.post("/assessments", response_model=AssessmentRead)
async def create_assessment(
data: AssessmentCreate,
current_user: User = Depends(get_current_active_user),
session: AsyncSession = Depends(get_session),
) -> AssessmentRead:
assessment = Assessment(**data.model_dump(), user_id=current_user.id)
session.add(assessment)
await session.commit()
await session.refresh(assessment)
return assessment
```
**Dependency chain:** `get_session` → `get_current_user` → `get_current_active_user`
---
## Pattern: Response Models
**Problem:** Inconsistent responses, exposing internal fields, poor OpenAPI docs.
```python
# ✅ CORRECT: Explicit response_model
@router.get("/users/{user_id}", response_model=UserRead)
async def get_user(
user_id: UUID,
session: AsyncSession = Depends(get_session),
) -> UserRead:
user = await get_user_or_404(user_id, session)
return user # Automatically filtered to UserRead fields
# ✅ CORRECT: List response
@router.get("/users", response_model=list[UserRead])
async def list_users(...) -> list[UserRead]:
...
# ✅ CORRECT: Paginated response
class PaginatedResponse(SQLModel, Generic[T]):
items: list[T]
total: int
page: int
size: int
@router.get("/assessments", response_model=PaginatedResponse[AssessmentRead])
async def list_assessments(...):
...
# ❌ WRONG: No response_model (exposes everything)
@router.get("/users/{user_id}")
async def get_user(user_id: UUID) -> User: # Exposes hashed_password!
...
```
**Why response_model matters:**
1. Filters output to only specified fields
2. Generates accurate OpenAPI schema
3. Frontend Orval codegen depends on this
---
## Pattern: Error Handling
**Problem:** Inconsistent error responses, missing context.
```python
from fastapi import HTTPException, status
# ✅ CORRECT: Specific HTTP exceptions
@router.get("/assessments/{id}")
async def get_assessment(id: UUID, session: AsyncSession = Depends(get_session)):
result = await session.execute(
select(Assessment).where(Assessment.id == id)
)
assessment = result.scalar_one_or_none()
if not assessment:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Assessment {id} not found",
)
return assessment
# ✅ CORRECT: Custom exception with handler
class AssessmentNotFoundError(Exception):
def __init__(self, assessment_id: UUID):
self.assessment_id = assessment_id
@app.exception_handler(AssessmentNotFoundError)
async def assessment_not_found_handler(request: Request, exc: AssessmentNotFoundError):
return JSONResponse(
status_code=404,
content={
"detail": f"Assessment {exc.assessment_id} not found",
"error_code": "ASSESSMENT_NOT_FOUND",
},
)
# ✅ CORRECT: Validation error detail
@router.post("/assessments")
async def create_assessment(data: AssessmentCreate):
if data.end_date < data.start_date:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="end_date must be after start_date",
)
```
**HTTP Status Codes:**
| Code | Use For |
|------|---------|
| 200 | Successful GET, PUT, PATCH |
| 201 | Successful POST (created) |
| 204 | Successful DELETE (no content) |
| 400 | Bad request (malformed) |
| 401 | Unauthorized (not authenticated) |
| 403 | Forbidden (authenticated but not allowed) |
| 404 | Not found |
| 422 | Validation error |
| 500 | Server error |
---
## Pattern: Route Ordering
**Problem:** FastAPI matches first route. Order matters for overlapping paths.
```python
# ❌ WRONG: Generic route before specific
@router.get("/users/{user_id}") # This catches "me" as user_id!
async def get_user(user_id: str):
...
@router.get("/users/me") # Never reached
async def get_current_user():
...
# ✅ CORRECT: Specific routes before generic
@router.get("/users/me") # Specific first
async def get_current_user():
...
@router.get("/users/{user_id}") # Generic after
async def get_user(user_id: UUID): # UUID type also helps
...
```
**Remember:** Always define specific routes before generic parameterized routes.
---
## Pattern: Path and Query Parameters
```python
# Path parameter - required, part of URL
@router.get("/users/{user_id}")
async def get_user(user_id: UUID): # /users/123
...
# Query parameters - optional, after ?
@router.get("/assessments")
async def list_assessments(
status: str | None = None, # /assessments?status=active
skip: int = 0, # /assessments?skip=10
limit: int = Query(default=20, le=100), # With validation
):
...
# Enum for constrained values
class AssessmentStatus(str, Enum):
DRAFT = "draft"
ACTIVE = "active"
COMPLETED = "completed"
@router.get("/assessments")
async def list_assessments(status: AssessmentStatus | None = None):
...
```
---
## Pattern: Request Body Validation
```python
from pydantic import Field, field_validator
class AssessmentCreate(SQLModel):
title: str = Field(min_length=1, max_length=200)
description: str | None = Field(default=None, max_length=1000)
skill_areas: list[str] = Field(min_length=1)
@field_validator("skill_areas")
@classmethod
def validate_skill_areas(cls, v: list[str]) -> list[str]:
valid_areas = {"fundamentals", "advanced", "strategy"}
for area in v:
if area not in valid_areas:
raise ValueError(f"Invalid skill area: {area}")
return v
# Automatic validation - returns 422 on failure
@router.post("/assessments", response_model=AssessmentRead)
async def create_assessment(data: AssessmentCreate):
...
```
---
## Pattern: Middleware
**Problem:** Cross-cutting concerns like logging, CORS, timing.
```python
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import time
app = FastAPI()
# CORS - order matters, add early
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000"], # Or ["*"] for dev
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Custom timing middleware
@app.middleware("http")
async def add_timing_header(request: Request, call_next):
start = time.time()
response = await call_next(request)
duration = time.time() - start
response.headers["X-Process-Time"] = str(duration)
return response
# Middleware order: Last added = First executed
```
---
## Pattern: OpenAPI Schema
**Problem:** Schema affects frontend codegen. Keep it clean.
```python
from fastapi import FastAPI
app = FastAPI(
title="My API",
version="1.0.0",
description="API description here",
)
# Good schema descriptions
class AssessmentCreate(SQLModel):
"""Create a new skill assessment."""
title: str = Field(description="AssessmenRelated in Design
contribute
IncludedLocal-only OSS contribution command center. Auto-refreshes the user's in-flight PR and issue state on invoke so conversations start with full context — no need to brief Claude on what's in flight. Helps the user find issues to contribute to on GitHub, builds per-repo dossiers of what each upstream expects (CLA, DCO, branch convention, AI policy, draft-first, review bots, issue templates), runs deterministic gates before any external action so AI-assisted contributions don't reach maintainers as slop. State is markdown-only: candidate files at ~/.contribute-system/candidates/, repo dossiers at ~/.contribute-system/research/, append-only event log at ~/.contribute-system/log.jsonl. No database, no cloud calls. Use when the user asks about their PRs / issues / contributions, wants to find new work to take on, claim an issue, build/refresh a repo's dossier, or draft a Design Issue or PR. Trigger with "/contribute", "what's my PR status", "find a contribution", "claim issue X", "draft a Design Issue for Y", "refresh dossier for Z".
architectural-analysis
IncludedUser-triggered deep architectural analysis of a codebase or scoped subtree across eight modes — information architecture, data flow, integration points, UI surfaces, interaction patterns, data model, control flow, and failure modes. This skill should be used when the user asks to "diagram this codebase," "map the architecture," "show the data flow," "give me an ERD," "trace control flow," "find the integration points," "verify the layout pattern," "audit the UX architecture," or any similar request whose primary deliverable is mermaid diagrams plus cited reports under docs/architecture/. Dispatches haiku/sonnet sub-agents in parallel for per-mode exploration, then verifies every citation mechanically before any node lands in a diagram. Not for one-off prose explanations of code (use code-explanation) or for high-level system design from scratch (use system-design).
mcp
IncludedModel Context Protocol (MCP) server development and tool management. Languages: Python, TypeScript. Capabilities: build MCP servers, integrate external APIs, discover/execute MCP tools, manage multi-server configs, design agent-centric tools. Actions: create, build, integrate, discover, execute, configure MCP servers/tools. Keywords: MCP, Model Context Protocol, MCP server, MCP tool, stdio transport, SSE transport, tool discovery, resource provider, prompt template, external API integration, Gemini CLI MCP, Claude MCP, agent tools, tool execution, server config. Use when: building MCP servers, integrating external APIs as MCP tools, discovering available MCP tools, executing MCP capabilities, configuring multi-server setups, designing tools for AI agents.
react-native-skia
IncludedDesign, build, debug, and optimise high-polish animated graphics in React Native or Expo using @shopify/react-native-skia, Reanimated, and Gesture Handler. Use when the user wants canvas-driven UI, shaders, paths, rich text, image filters, sprite fields, Skottie, video frames, snapshots, web CanvasKit setup, or performance tuning for custom motion-heavy elements such as loaders, hero art, cards, charts, progress indicators, particle systems, or gesture-driven surfaces. Also use when the user asks for fluid, glow, glass, blob, parallax, 60fps/120fps, or GPU-friendly animated effects in React Native, even if they do not explicitly say "Skia". Do not use for ordinary form/layout work with standard views.
plaid
IncludedProduct Led AI Development — guides founders from idea to launched product. Six capabilities: Idea (discover a product idea), Validate (pressure-test the idea against fatal flaws, problem reality, competition, and 2-week MVP feasibility), Plan (vision intake + document generation), Design (translate image references into a design.md spec), Launch (go-to-market strategy), and Build (roadmap execution). Use when someone says "PLAID", "plaid idea", "help me find an idea", "product idea", "idea from my business", "idea from my expertise", "plaid validate", "validate my idea", "pressure-test", "is this idea good", "find fatal flaws", "validate the problem", "plan a product", "define my vision", "generate a PRD", "product strategy", "plaid design", "design from image", "translate image to design", "create design.md", "extract design tokens", "plaid launch", "go-to-market", "launch plan", "GTM strategy", "launch playbook", "plaid build", "build the app", "start building", or "execute the roadmap".
nextjs-framer-motion-animations
IncludedAdds production-safe Motion for React or Framer Motion animations to Next.js apps, including reveal, hover and tap micro-interactions, whileInView, stagger, AnimatePresence, layout and layoutId transitions, reorder, scroll-linked UI, and lightweight route-content transitions. Use when the user asks to add, refactor, or debug Motion or Framer Motion in App Router or Pages Router codebases, especially around server/client boundaries, reduced motion, LazyMotion, bundle size, hydration, or route transitions. Avoid for GSAP-style timelines, WebGL or 3D scenes, heavy scroll storytelling, or CSS-only effects unless Motion is explicitly requested.