FASTAPI AGENTS.MD

A Python API service on FastAPI. The rules keep an agent on Pydantic v2 and modern SQLAlchemy, prevent blocking calls inside async endpoints, and make dependency injection the way to reach the database.

Zero retention: everything you type stays in this browser. Verify it yourself in DevTools.
AGENTS.mdFor Codex, Cursor, Copilot, Jules, Windsurf and more
# AGENTS.md

Project: fastapi-service. A FastAPI service with async SQLAlchemy and PostgreSQL.

This file is the single source of truth for every coding agent working in this repository. The tool-specific files (CLAUDE.md, GEMINI.md, .github/copilot-instructions.md and .cursor/rules/project.mdc) point here. Edit this file, not those.

## Stack

- Language: Python
- Framework: FastAPI
- Database: PostgreSQL
- Hosting: Fly.io
- Tests: pytest
- Package manager: uv

## Commands

Run these from the repository root.

- Install dependencies: `uv sync`
- Run locally: `uv run fastapi dev app/main.py`
- Run all tests: `uv run pytest`
- Run one test file: `uv run pytest tests/test_items.py::test_create_item`
- Lint: `uv run ruff check .`
- Type-check: `uv run mypy app`
- Format: `uv run ruff format .`

## Project layout

- `app/main.py`: App factory and router registration
- `app/routers/`: Endpoint modules (APIRouter)
- `app/schemas/`: Pydantic request and response models
- `app/models/`: SQLAlchemy ORM models
- `app/deps.py`: Dependencies: DB session, current user
- `alembic/versions/`: Database migrations

## Code style

- Indent with 4 spaces. Never tabs.
- Every new or changed Python function has type hints for its parameters and return value.
- Match the style and patterns of the surrounding code, even where you would personally choose differently.
- Prefer small pure functions and immutable data; keep side effects at the edges (handlers, jobs, I/O modules).
- Never swallow an error. Handle it where you can act on it, or rethrow it with context about what failed.
- Commit messages follow Conventional Commits: `feat:`, `fix:`, `refactor:`, `test:`, `docs:`, `chore:`, with an imperative summary under 72 characters.

## Working rules

- Before you say a task is done, run the test, lint and type-check commands above and make sure they pass. If you could not run them, say so.
- For a bug fix or behavior change, first write or update a test that fails, then make it pass.
- Never write secrets, API keys, tokens or passwords into code, tests, fixtures, logs or commits. Read them from environment variables, and never commit `.env` files.
- Do not add a new dependency without asking first. Prefer the standard library and what is already installed.
- Ask before creating, editing or running database migrations, and never edit a migration that has already been applied.
- Change only what the task needs. Do not refactor, rename or reformat unrelated code.

## Product constraints

- Validate all input at the boundary, use parameterized queries only, and treat every external payload as untrusted.

## Project-specific notes

- Use Pydantic v2 APIs: `model_validate`, `model_dump`, `ConfigDict`, `field_validator`. Never the v1 names (`.dict()`, `.parse_obj`, `class Config`, `@validator`).
- Every endpoint declares a `response_model` (or return type) so internal fields are never serialized by accident.
- Get the database session and the current user through `Depends`, never by importing a global session.
- Inside `async def` endpoints, never call blocking I/O (`requests`, sync DB drivers, `time.sleep`); use async clients such as `httpx.AsyncClient`, or make the endpoint a plain `def`.
- Use SQLAlchemy 2.0 style: `select()` with `session.execute`, `Mapped[...]` annotations; not the legacy `session.query`.
- Schema changes go through `uv run alembic revision --autogenerate -m "..."`; review the generated file before committing.
- Tests use `httpx.AsyncClient` or `TestClient` with dependency overrides for the database.

## Project phase

This project is in active development. Follow the patterns that already exist; consistency beats novelty.
CLAUDE.mdFor Claude Code
# CLAUDE.md

@AGENTS.md

## Claude Code

- The shared project instructions are imported from AGENTS.md on the line above. Edit AGENTS.md, not this file, so every agent stays in sync.
- Put Claude-only preferences below this line. Personal, uncommitted notes belong in CLAUDE.local.md.
GEMINI.mdFor Gemini CLI
# GEMINI.md

@./AGENTS.md

## Gemini CLI

- The shared project instructions are imported from AGENTS.md on the line above. Edit AGENTS.md, not this file.
- Alternative: delete this file and set "context": { "fileName": ["AGENTS.md"] } in .gemini/settings.json so Gemini CLI reads AGENTS.md directly.
.github/copilot-instructions.mdFor GitHub Copilot
# Copilot instructions

Project: fastapi-service. A FastAPI service with async SQLAlchemy and PostgreSQL.

The full instructions are in AGENTS.md at the repository root. Read it before you change code; if anything here disagrees with it, AGENTS.md wins. The essentials are repeated below for Copilot surfaces that only read this file.

## Stack

- Language: Python
- Framework: FastAPI
- Database: PostgreSQL
- Hosting: Fly.io
- Tests: pytest
- Package manager: uv

## Commands

- Install dependencies: `uv sync`
- Run locally: `uv run fastapi dev app/main.py`
- Run all tests: `uv run pytest`
- Run one test file: `uv run pytest tests/test_items.py::test_create_item`
- Lint: `uv run ruff check .`
- Type-check: `uv run mypy app`
- Format: `uv run ruff format .`

## Working rules

- Before you say a task is done, run the test, lint and type-check commands above and make sure they pass. If you could not run them, say so.
- For a bug fix or behavior change, first write or update a test that fails, then make it pass.
- Never write secrets, API keys, tokens or passwords into code, tests, fixtures, logs or commits. Read them from environment variables, and never commit `.env` files.
- Do not add a new dependency without asking first. Prefer the standard library and what is already installed.
- Ask before creating, editing or running database migrations, and never edit a migration that has already been applied.
- Change only what the task needs. Do not refactor, rename or reformat unrelated code.
.cursor/rules/project.mdcFor Cursor
---
description: Project instructions for every task (mirrors AGENTS.md)
globs:
alwaysApply: true
---

# Project rules

The project instructions live in @AGENTS.md at the repository root, which Cursor loads automatically. They are not repeated here, so they cost context only once. Edit AGENTS.md, not this rule.

Before you change code, check the Commands and Working rules sections of AGENTS.md, and run its test and lint commands before you say a task is done.

Put Cursor-only rules below, or add scoped rules as more .mdc files with `globs` (for example `src/components/**/*.tsx`).

These are the exact files the generator writes for this preset. Change any command or rule in the generator and download your own set.

About the FastAPI preset

What Is in the FastAPI AGENTS.md?

The preset describes a a FastAPI service with async SQLAlchemy and PostgreSQL. Its AGENTS.md is 52 non-empty lines with 7 exact commands, 6 key directories, and 7 stack-specific rules on top of the shared working rules.

  • Commands: uv sync, uv run fastapi dev app/main.py, uv run pytest, uv run pytest tests/test_items.py::test_create_item, uv run ruff check ., uv run mypy app, uv run ruff format ..
  • Layout: app/main.py, app/routers/, app/schemas/, app/models/, app/deps.py, alembic/versions/.
  • Wrappers: CLAUDE.md and GEMINI.md import AGENTS.md; the Copilot file repeats the essentials; the Cursor rule points to AGENTS.md.

Why These Rules for FastAPI?

Pydantic v1 versus v2

Pydantic v2 renamed most of its methods. Agents mix both, and v1 names either fail or emit deprecation warnings. Listing the banned names is the quickest fix.

Blocking calls freeze the event loop

One requests.get inside an async endpoint stalls every other request on that worker. It works fine in a single-user test, which is why agents do not notice.

response_model is a privacy filter

Returning an ORM object without a response model can serialize password hashes and internal flags. The declared model whitelists fields.

Autogenerate needs review

Alembic autogenerate misses some changes and can propose destructive drops; the rule asks for a human-readable review.

Which Stack Rules Does the File Add?

  • Use Pydantic v2 APIs: model_validate, model_dump, ConfigDict, field_validator. Never the v1 names (.dict(), .parse_obj, class Config, @validator).
  • Every endpoint declares a response_model (or return type) so internal fields are never serialized by accident.
  • Get the database session and the current user through Depends, never by importing a global session.
  • Inside async def endpoints, never call blocking I/O (requests, sync DB drivers, time.sleep); use async clients such as httpx.AsyncClient, or make the endpoint a plain def.
  • Use SQLAlchemy 2.0 style: select() with session.execute, Mapped[...] annotations; not the legacy session.query.
  • Schema changes go through uv run alembic revision --autogenerate -m "..."; review the generated file before committing.
  • Tests use httpx.AsyncClient or TestClient with dependency overrides for the database.

How Do You Add These Files to a FastAPI Repository?

  1. Download the zip above. It is laid out exactly as the files go into the repository.
  2. Unzip it at the repository root. AGENTS.md, CLAUDE.md and GEMINI.md go next to your README; the Copilot file goes in .github/ and the Cursor rule in .cursor/rules/.
  3. Open AGENTS.md and check every command against your package scripts or Makefile.
  4. Delete wrappers for tools your team does not use, and any old .cursorrules or .windsurfrules file.
  5. Commit, then run the linter on AGENTS.md whenever you change it.

Want different commands or rules? Load this preset into the generator, change anything, and download your own set.

Which Agents Will Read These Files?

AGENTS.md is read directly by Codex, Cursor, Windsurf, Cline, Zed and Jules, and by GitHub Copilot's cloud agent, code review and CLI. Claude Code reads it through the generated CLAUDE.md, and Gemini CLI through GEMINI.md. See the compatibility table for details, last verified 2026-11-13.

Which Similar Stacks Are in the Library?

All presets are on the stacks page.

Frequently Asked Questions

Why is there no build command?

A FastAPI service is run, not compiled. The test, lint and type-check commands are the verification steps; add a Docker build command in the generator if you deploy a container.

Does this work with SQLModel?

Yes, with small changes: SQLModel builds on SQLAlchemy and Pydantic. Note it in the project-specific notes so the agent uses SQLModel classes.

Should endpoints be async or sync?

Use async when the endpoint awaits async I/O. If a library you call is synchronous, a plain def endpoint runs in a threadpool and is the safer choice.

Are these FastAPI files free to use?

Yes. Copy them into any project, commercial or not, without attribution. They are a starting point: adjust the commands and rules to match your repository.

How Can You Verify That Nothing Leaves This Browser?

You do not have to take our word for it. Developer tools show every request a page makes, so you can watch this page work and confirm that nothing you type is sent. The check takes under a minute.

  1. Open your browser developer tools with F12, or Cmd+Option+I on a Mac, and select the Network tab.
  2. Reload the page and let it finish. You will see the page's own files, its web fonts, and one anonymous page-view request. Nothing has been typed yet, so none of them can contain your input.
  3. Click the clear icon in the Network tab so that the log is empty.
  4. Switch between the file tabs, copy a file and download the zip.
  5. Look at the Network tab again. It is still empty. Downloads are built in the page as Blob URLs, so even the zip never touches a server. For extra proof, turn on offline mode after the page loads: everything keeps working.

Only the feedback form can add a line to that log, and it sends only the vote and comment you type into it. 2prompt has no server code at all: no API, no model and no database of your files. The production site also sends a Content-Security-Policy header that tells your browser to refuse connections to anywhere except this site, the fonts and the anonymous analytics endpoint.

Was this helpful?