fastapi

A set of conventions for building and reviewing FastAPI services, a Python framework for creating web APIs. It covers application structure, routes, dependencies, request and response data, streaming endpoints, and API tests.

In plain words
What is it for?
Use it when creating or updating FastAPI applications, route handlers, dependency injection, data schemas, streaming APIs, or API tests.
Why use it?
It gives FastAPI projects a consistent layout and helps avoid unclear routes, mixed data models, and difficult-to-test service code.

Command for Gemini CLI

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add commands/mfmezger/ai_agent_dotfiles/fastapi
Clone the repo
git clone --depth 1 https://github.com/mfmezger/ai_agent_dotfiles

Made for: Gemini CLI.

Per session 84 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 1,207 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00084 $0.01207
Opus 5 $0.00042 $0.00603
Sonnet 5 $0.00017 $0.00241
Haiku 4.5 $0.00008 $0.00121

Measured 3d ago against content hash 086b52c06233, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

fastapi scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 3d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

gemini/.gemini/commands/fastapi.md · 167 lines

How it starts

The opening of the file, as written. The whole thing — 167 lines — stays where its author put it; the contents beside it link to each section on GitHub.

FastAPI Conventions

Assume the general Python tooling conventions from the python-stack skill. This skill only covers FastAPI-specific patterns.

App Layout

Prefer a small, explicit structure:

src/<package>/
  main.py
  routers/
    __init__.py
    items.py
  dependencies.py
  schemas.py
  models.py
  settings.py
  • Keep the ASGI app in main.py
  • Group path operations into routers by bounded area
  • Put shared dependency helpers in dependencies.py
  • Keep request/response schemas separate from persistence models when that improves clarity

Running the App

Prefer the FastAPI CLI over invoking Uvicorn directly.

uv run fastapi dev src/<package>/main.py
uv run fastapi run src/<package>/main.py

If the project has a stable app entrypoint, prefer configuring it in pyproject.toml:

[tool.fastapi]
entrypoint = "src.<package>.main:app"

Path Operations

Use one HTTP operation per function. Do not collapse multiple methods into a single handler.

Use Annotated for request parameters and dependencies:

from typing import Annotated

from fastapi import APIRouter, Depends, Path, Query

router = APIRouter(prefix="/items", tags=["items"])

ItemId = Annotated[int, Path(ge=1)]
SearchQuery = Annotated[str | None, Query(max_length=100)]


@router.get("/{item_id}")
async def get_item(item_id: ItemId, q: SearchQuery = None) -> dict[str, str | int | None]:
    return {"item_id": item_id, "q": q}
  • Prefer reusable type aliases for common dependencies and parameter declarations
  • Do not use ... as a required marker in FastAPI parameters or Pydantic fields
  • Do not use @app.api_route(..., methods=[...]) unless there is a strong reason

Request and Response Models

Declare response types deliberately.

  • Prefer a concrete return type when the returned value already matches the public schema
  • Use response_model= when the runtime return value differs from the public schema
  • Treat response models as a data-exposure boundary; never return raw internal models that contain secrets or extra fields
  • Prefer regular Pydantic models or standard typed containers over RootModel

Read the full file on GitHub · 167 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 3d ago First seen · 167 lines · 84 tokens per session scan A 086b52c06233

Subscribe to this mod's changes

fastapi is a command published in the GitHub repository mfmezger/ai_agent_dotfiles (5 stars, last pushed 1mo ago), licensed MIT. It adds 84 tokens to every session and 1,207 once invoked, about $0.0004 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.