AgentLint Verified 100/100Verified Framework Starter (Python 929-repo corpus)

    FastAPI (Python) CLAUDE.md & AGENTS.md Template

    Async/sync boundary safety, Pydantic v2 validation, Alembic migrations, and fast pytest loops for FastAPI services.

    # FastAPI Service — Python API Harness
    
    Python 3.12 FastAPI backend using Pydantic v2, SQLAlchemy 2.0 async, Alembic migrations, and `uv` + `pytest`.
    
    ## Commands
    
    - Sync deps: `uv sync --dev`
    - Fast unit test: `uv run pytest tests/unit/test_orders.py -x -q -m "not slow and not network"`
    - Full suite: `uv run pytest -q`
    - Lint & typecheck: `uv run ruff check . && uv run ruff format --check . && uv run pyright`
    - Run dev server: `uv run uvicorn app.main:app --reload`
    
    ## Rules
    
    - Don't run blocking HTTP (`requests`) or synchronous DB queries inside `async def` routes. Instead use `httpx.AsyncClient` and async SQLAlchemy sessions, or declare the handler as `def`. Because blocking calls stall the entire Uvicorn event loop.
    - Don't return raw ORM models from endpoints. Instead declare explicit Pydantic v2 `response_model` schemas in `app/schemas/`. Because unguarded ORM serialization leaks internal columns and triggers lazy-load errors.
    - Don't invoke global `python` or `pip`. Instead prefix every command with `uv run`. Because host Python versions diverge from `uv.lock`.
    - When editing SQLAlchemy models, read `docs/alembic-migrations.md` before generating revisions.
    
    ## Continuity & Safety
    
    - Keep `.env`, `.venv/`, and `CLAUDE.local.md` in `.gitignore` — load secrets via `pydantic-settings`.
    - Record passing `pytest` counts and migration IDs in `HANDOFF.md`.

    Most Adopted FastAPI (Python) Rules Across Real Open-Source Repos

    Adoption benchmarks measured in the parent language cohort of our 4,533-repo harness study, paired with framework-specific invariants:

    pytesttesting

    pytest test suite (measured in 929 Python corpus repos)

    45.9%426 repos
    type-hintspatterns

    Strict type hints & Pydantic schemas (929 Python corpus repos)

    29.8%277 repos
    rufftooling

    Ruff lint and format enforcement (929 Python corpus repos)

    29.1%270 repos
    dockertooling

    Containerized API deployment (929 Python corpus repos)

    28.6%266 repos
    uvtooling

    uv virtual environment isolation (929 Python corpus repos)

    24.5%228 repos
    Essential Commands (Checks W1 & W7)
    install
    uv sync --dev
    fastTest
    uv run pytest tests/unit/test_routes.py -x -q -m 'not slow'
    fullTest
    uv run pytest -q
    lint
    uv run ruff check . && uv run ruff format --check . && uv run pyright
    build
    uv run fastapi check app/main.py
    Common FastAPI (Python) Agent Pitfalls
    • 01Agents place blocking requests.get() or sync ORM calls inside async def endpoints, freezing the entire Uvicorn event loop.
    • 02Returning raw SQLAlchemy models without Pydantic response_model leaks internal columns like password hashes.
    • 03Skipping Alembic migration autogeneration leaves DB schemas out of sync with SQLAlchemy models.

    More Verified Stack Templates