fastapi

Guides production-grade FastAPI development covering dependencies, async correctness, validation, security, and deployment.

5|Updated Jan 31, 2026
One-click install
npx skills add https://github.com/nuggocto/dotfiles --skill fastapi-nuggocto
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: fastapi
Source: https://github.com/nuggocto/dotfiles/tree/main/opencode/skills/fastapi
Command: npx skills add https://github.com/nuggocto/dotfiles --skill fastapi-nuggocto

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Building FastAPI services that survive production requires correct handling of async execution, dependency lifecycles, Pydantic validation, security boundaries, and deployment topology—mistakes in any of these cause subtle bugs, leaks, or outages. This Skill encodes version-aware, production-grade guidance so those decisions are made correctly the first time. ## Core Features & Use Cases - Lifecycle and dependency correctness: Enforces proper use of lifespan-managed process resources, request-scoped yield dependencies, and per-request database sessions with deterministic teardown. - Async and blocking discipline: Prevents blocking work on the event loop, governs thread-pool offloading, streaming, WebSockets, and background task durability boundaries. - Contract and security rigor: Covers Pydantic v2 validation patterns, response model filtering, stable error envelopes, OIDC/JWT verification, CORS, proxy trust, and OpenAPI contract testing. - Use Case: When adding a streaming endpoint with authentication to an existing FastAPI service, the Skill ensures resources survive the stream, headers are sent only after validation, and the OpenAPI contract stays accurate. ## Quick Start Ask the assistant to review or implement a FastAPI endpoint, router, or deployment configuration using the fastapi skill for production-correct patterns.

Frequently Asked Questions about fastapi

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I manage database sessions in FastAPI dependencies?

Create one session per request through a yield dependency and close it deterministically in finally. Use a process-wide engine created in lifespan, let the application operation own commit and rollback, and never share sessions across concurrent requests.

How do I run blocking code in FastAPI without blocking the event loop?

Use an ordinary def handler so FastAPI offloads it to the thread pool, or call a bounded AnyIO thread function explicitly. Never run blocking database calls, file I/O, or CPU-heavy work directly inside async def handlers.

Should I use SQLModel or SQLAlchemy with FastAPI?

SQLModel suits straightforward new services by reducing model duplication, while SQLAlchemy directly is better when you need its mapper, async, or ecosystem features. Either way, use Alembic for migrations and map to explicit Pydantic response models before returning data.

Does FastAPI validate streaming responses with response models?

Raw StreamingResponse chunks bypass response-model validation. Native typed JSONL and SSE generator endpoints validate and serialize each yielded item on FastAPI 0.140.13 or newer, while raw chunks remain application-owned.

When should I use BackgroundTasks versus a task queue in FastAPI?

BackgroundTasks suit short, best-effort work whose loss on crash is acceptable, since they run in-process after the response. Use a durable queue with a transactional outbox for billing, webhooks, retries, or anything requiring guaranteed execution.

Why does my FastAPI dependency cleanup run after the response is sent?

Yield dependencies default to request scope and clean up after the response finishes; with function scope, serialization happens before teardown. Keep resources needed by streams in request scope and verify ordering against your pinned FastAPI version.