aiwf-fastapi-coding
Use for FastAPI routes, Pydantic models, OpenAPI contracts, async boundaries, auth/CORS, and API validation.
SKILL.mdskills/aiwf-fastapi-coding/SKILL.md
GitHubAIWF FastAPI Coding
Core Rule
Treat FastAPI work as a typed API contract. Inspect FastAPI, Starlette, Pydantic, Uvicorn, dependency injection, and deployment settings before changing routes or models. Keep API behavior verifiable through OpenAPI, tests, and endpoint smokes.
Workflow
- Inspect
pyproject.toml, requirements, app entrypoints, routers, dependency providers, settings, middleware, tests, and deployment commands. - Confirm Pydantic major version and FastAPI version before editing validators, model config, response models, or serialization behavior.
- Preserve request/response contracts unless the task explicitly changes them. Update tests and OpenAPI-facing docs when contracts move.
- Keep async routes non-blocking. Move blocking CPU, file, model, or network work into the project's existing worker/thread/process pattern.
- Validate with the repo's API tests and a health or contract smoke.
FastAPI Guardrails
- Use typed request bodies, path/query parameters, dependencies, and response models. Avoid loose
dictcontracts unless the API is intentionally schema-less. - Treat Pydantic v1 examples as legacy and version-bound. Current FastAPI releases require Pydantic v2; inspect pinned versions before migrating old validators or models.
- Keep auth, CORS, CSRF, session, cookie, and token behavior explicit. Add
aiwf-security-guardrailswhen those surfaces are touched. - Prefer lifespan handlers for new startup/shutdown work and preserve the app's existing lifecycle pattern. Do not mix lifespan and event handlers or duplicate global initialization paths.
- Keep exception handlers and status codes consistent with existing API behavior.
- For streaming, WebSockets, SSE, upload, and background tasks, validate cancellation and resource cleanup.
- Avoid silently changing generated OpenAPI names that clients may depend on.
Validation Defaults
Prefer existing commands. Useful fallbacks:
python -m compileall <package-or-file>
python -m pytest
python -m uvicorn <module>:<app> --host 127.0.0.1 --port <port>
When starting a server, keep it local unless Shawn asks otherwise. For contract checks, use TestClient, httpx, or the repo's smoke client.
Primary Source Anchors
- FastAPI documentation: https://fastapi.tiangolo.com/
- FastAPI tutorial and user guide: https://fastapi.tiangolo.com/tutorial/
- FastAPI release notes: https://fastapi.tiangolo.com/release-notes/
FastAPI and Pydantic behavior changes by version. Verify the installed versions before making version-specific edits.