agentleFS
qyndexqyndex/claude-init/.claude/skills/fastapi/SKILL.md

fastapi

Skill2 starsChanged 5 months agoMITView on GitHub

Use when project is FastAPI (Python async web framework). Covers Pydantic v2 models, dependency injection via Depends, async SQLAlchemy 2.x, lifespan context manager, BackgroundTasks vs Celery, routers per feature. Round 9 B.

27 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use docker, react and 3 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • wrsmith108Skill

    docker

    Container-based development for isolated, reproducible environments. Use when running npm commands, installing packages, executing code, or managing project dependencies. Trigger phrases include "npm install", "run the build", "start the server", "install package", or any code execution request.

    Also in 4 projects that use this

  • VercelSkill

    react

    React renderer for json-render that turns JSON specs into React components. Use when working with @json-render/react, building React UIs from JSON, creating component catalogs, or rendering AI-generated specs.

    Also in 3 projects that use this

  • rogue-dev-studioSkill

    python

    Python 3.13 with uv package manager via mise. Use when the project needs this capability or the user / team manifest asks for it. Use for specialized `python` work when listed in TEAM.yaml or explicitly requested.

    Also in 3 projects that use this

  • MicrosoftSkill

    typescript

    TypeScript best practices for this monorepo. Use this skill when: (1) writing new TypeScript code, (2) reviewing TypeScript code, (3) working with API responses, fetch calls, or JSON parsing, (4) adding or modifying types and interfaces.

    Also in 3 projects that use this

  • BlockSkill

    frontend-design

    Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.

    Also in 3 projects that use this

The file

We split this skill into the rules it gives an agent

4 rules. 0 are common, 1 is distinctive, and 3 are very specific to this project.

  • 0

    Common

    Most of the 25 edited versions we read keep these.

  • 1

    Distinctive

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Request bodies are Pydantic classes: Never dict or Body() without a model.

    line 20
  2. Very specific:

    Dependency injection via Depends(): No module-level state for DB session / current user / settings.

    line 21
  3. Very specific:

    Every endpoint has response_model=

    line 96
  4. Distinctive:

    All endpoints have typed request + response models

    line 120

“Common” is measured: kept by at least 6 in 10 of the 25 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

FastAPI

Mandate

Enforce typed-everywhere Pydantic v2, dependency injection via Depends(), async SQLAlchemy 2.x patterns, modern lifespan (NOT on_startup/on_shutdown), and BackgroundTasks-vs-Celery decision discipline.

Decisions encoded

  • Pydantic v2 only. v1 patterns (e.g., BaseModel.dict()) are anti-patterns; use model_dump().
  • Every endpoint declares response_model=. Untyped responses → OpenAPI lies.
  • Request bodies are Pydantic classes. Never dict or Body() without a model.
  • Dependency injection via Depends(). No module-level state for DB session / current user / settings.
  • Routers per feature. APIRouter(prefix="/users", tags=["users"]); main.py only mounts routers.
  • @asynccontextmanager lifespan. Deprecated on_startup/on_shutdown are forbidden.
  • BackgroundTasks for fire-and-forget ≤ 30s; Celery/RQ for retries, scheduling, or > 30s.
  • Async SQLAlchemy 2.x. select(User).where(...) style — NOT legacy Query.

Project structure

app/
├── main.py              # FastAPI() instance, lifespan, router mounts
├── api/
│   └── v1/
│       ├── __init__.py  # APIRouter(prefix="/v1")
│       ├── users.py
│       ├── auth.py
│       └── billing.py
├── core/
│   ├── config.py        # Pydantic Settings
│   ├── deps.py          # Depends() factories
│   ├── security.py      # JWT, password hashing
│   └── db.py            # async engine + sessionmaker
├── models/              # SQLAlchemy 2.x DeclarativeBase
├── schemas/             # Pydantic request/response models
├── services/            # business logic; no FastAPI imports
└── tests/
    ├── conftest.py      # test client + db fixtures via testcontainers
    ├── test_users.py
    └── test_auth.py

Canonical patterns

Lifespan (NOT on_startup/on_shutdown)

from contextlib import asynccontextmanager
from fastapi import FastAPI

@asynccontextmanager
async def lifespan(app: FastAPI):
    # startup
    app.state.engine = create_async_engine(...)
    yield
    # shutdown
    await app.state.engine.dispose()

app = FastAPI(lifespan=lifespan)

Dependency injection

async def get_db(request: Request) -> AsyncSession:
    async with AsyncSession(request.app.state.engine) as session:
        yield session

@router.post("/users", response_model=UserResponse, status_code=201)
async def create_user(
    body: UserCreate,                    # Pydantic — validated at boundary
    db: AsyncSession = Depends(get_db),
    current: User = Depends(get_current_user),
) -> UserResponse:
    ...

Async SQLAlchemy 2.x

result = await db.execute(
    select(User).where(User.email == email)
)
user = result.scalar_one_or_none()

Hard rules

  • Pydantic v2 (from pydantic import BaseModel + model_config, NOT Config class)
  • Every endpoint has response_model=
  • No dict request bodies — always Pydantic
  • No Session() instantiation outside Depends(get_db)
  • No Query()-style legacy SQLAlchemy
  • Services NEVER import from fastapi; routes call services
  • BackgroundTasks(.add_task) only for ≤ 30s + idempotent + crash-safe; otherwise Celery/RQ

Common pitfalls

  1. Sync function with async DB — silent deadlock or broken connection pool. Async all the way down.
  2. Sharing a session across requests — use Depends(get_db) per request; never module-level session.
  3. on_startup / on_shutdown — deprecated; use lifespan.
  4. Returning ORM objects directly — Pydantic doesn't serialize; declare response_model + use from_attributes=True in model_config.
  5. Body() for complex bodies — use a Pydantic class so OpenAPI is accurate.
  6. CPU-bound work in BackgroundTasks — blocks the event loop; offload to Celery + worker.

References

Done means

  • All endpoints have typed request + response models
  • Lifespan replaces on_startup/on_shutdown
  • DB session via Depends, never module-level
  • Routers organized by feature, mounted from main.py

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.