Fastapi patterns
Skill weorbitant/compound-engineering-feat-python-plugin/skills/fastapi-patterns
FastAPI core patterns for routes, dependency injection, async, auth, and OpenAPI. Use when working on projects with fastapi in their dependencies.From its SKILL.md
npx -y skills add weorbitant/compound-engineering-feat-python-plugin --skill fastapi-patternsAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 1 stars1 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
2.8 KB, as published. Nobody here has run it
FastAPI Patterns
Patterns and conventions for building FastAPI applications. Follow the principle of explicit dependency injection, leverage async where it matters, and use Pydantic models as the single source of truth for request/response schemas.
Core Principles
- Dependency injection everywhere -- Use
Depends()for database sessions, auth, config, and shared logic instead of global state or imports - Pydantic as contract -- Define request and response models explicitly with Pydantic; avoid raw dicts in route signatures
- Async when beneficial -- Use
async deffor I/O-bound routes with async drivers; use plaindeffor CPU-bound or sync-only code - Router organization -- Split routes into
APIRoutermodules by domain, compose them in the main app with prefixes and tags
When to Use
Apply these patterns when working on any project that lists fastapi in its dependencies.
Detect this by checking pyproject.toml, requirements.txt, or Pipfile for FastAPI.
Type Hints
Use | None instead of Optional for all type annotations:
from fastapi import Query
async def list_items(
category: str | None = Query(None, description="Filter by category"),
) -> list[ItemResponse]:
...
Logging
Use emojis as prefixes in log messages:
logger.info("✅ Request processed for %s", endpoint)
logger.warning("⚠️ Rate limit approaching for client %s", client_id)
logger.error("❌ Failed to connect to database: %s", exc)
Project Structure
src/
├── main.py # App factory, lifespan, router composition
├── config.py # Settings with pydantic-settings
├── dependencies.py # Shared dependencies (DB session, current user)
├── routes/
│ ├── __init__.py
│ ├── users.py # APIRouter for user endpoints
│ └── items.py # APIRouter for item endpoints
├── models/ # SQLAlchemy or ORM models
├── schemas/ # Pydantic request/response models
├── services/ # Business logic layer
└── middleware/ # Custom middleware
Reference Documents
- routes-di.md -- Routes, dependency injection, middleware, error handling, OpenAPI customization, lifespan events
- async-patterns.md -- Async/await patterns, background tasks, WebSockets, streaming, connection pooling, testing
- auth.md -- Authentication and authorization: OAuth2, JWT, API keys, RBAC, scopes, password hashing, CORS for auth