FastAPIRule
by DUOMReport
Rules for FastAPI services: Pydantic models, dependencies, async I/O, and safe error responses.
Developer toolsLicense: CC0-1.0Updated yesterdayrule/fastapi
Install
Run in your project folder (DuoM CLI: risk check, then write; undo with npx duom@1 undo)
npx duom@1 add rule/fastapi --client claude-code
Writes to CLAUDE.md in your project (where Claude Code reads it). If the project has an AGENTS.md but no CLAUDE.md, DuoM creates CLAUDE.md starting with @AGENTS.md so Claude Code keeps reading it. Only the marked block is added; the rest of the file is left alone, and remove takes out only that block.
Manual: paste into CLAUDE.md
CLAUDE.md
<!-- duom:rule/fastapi --> ## FastAPI - Define request and response bodies as Pydantic models; never return raw ORM objects. - Share DB sessions, auth and settings through Depends(), not globals. - Use async def only when everything inside awaits; blocking calls go in plain def handlers or a thread pool. - Raise HTTPException with a clear status code; do not leak stack traces or internal errors to clients. - Keep settings in a pydantic-settings class loaded from the environment. - Test endpoints with TestClient or httpx.AsyncClient. <!-- /duom:rule/fastapi -->
The rule
FastAPI
- Define request and response bodies as Pydantic models; never return raw ORM objects.
- Share DB sessions, auth and settings through Depends(), not globals.
- Use async def only when everything inside awaits; blocking calls go in plain def handlers or a thread pool.
- Raise HTTPException with a clear status code; do not leak stack traces or internal errors to clients.
- Keep settings in a pydantic-settings class loaded from the environment.
- Test endpoints with TestClient or httpx.AsyncClient.