Backend · Cursor Rules

Python + FastAPI Rules

Cursor rules for Python + FastAPI backends. Async-first, Pydantic v2, SQLAlchemy 2.0, structured errors.

Python · FastAPI

What it does

Teaches Cursor to default to async def for all IO, use Pydantic v2 models for every request and response, stick to SQLAlchemy 2.0 async style with Alembic migrations, enforce type hints with mypy strict, and test with pytest-asyncio plus httpx AsyncClient.

How to install

Which tool are you using?

Not sure? Claude.ai is the website. Claude Code is the command-line tool you install separately. Cursor is a code editor that reads .cursorrules.

  1. 01

    Copy the rules

    Click the Copy button on the code block below to grab the full contents of .cursorrules.

  2. 02

    Create .cursorrules at your project root

    Cursor reads .cursorrules from the top-level folder of your project. Paste the copied content there.

    terminal
    touch .cursorrules
  3. 03

    Reload Cursor

    Cmd+Shift+P → "Developer: Reload Window". Cursor picks up the new rules immediately.

  4. 04

    Verify with a test prompt

    Ask Cursor to generate a component or function in your stack. The output should follow the conventions in your rules file.

The cursor rules file

Copy the full contents below, or download the file directly.

.cursorrules
.cursorrules
# Python + FastAPI Cursor Rules You are an expert Python backend developer using FastAPI. ## Structure- app/api/v1/routers for endpoints- app/services for business logic- app/models for SQLAlchemy / Pydantic- app/schemas for request/response models ## Types- Type hints on every function signature- Pydantic v2 models for all I/O- No Dict[str, Any] in public APIs- mypy strict on the services layer ## Async- async def for all IO-bound code- httpx for outbound calls, not requests- Never call sync DB from async handler- Use background tasks for fire-and-forget ## Database- SQLAlchemy 2.0 style with async engine- Alembic for migrations- Session per request via Depends- No ORM queries in routers ## Errors- HTTPException with consistent { code, message } body- Log with structlog + correlation id- Never return stack traces- 4xx for client errors, 5xx for ours ## Testing- pytest + pytest-asyncio- httpx AsyncClient for integration- Factories over fixtures for test data- Coverage 80%+ on services

Example output

What Claude does before and after you install this cursor rules.

Without this cursor rules

Cursor writes sync def endpoints that block the event loop, uses requests instead of httpx, and skips type hints on response models.

With this cursor rules

Cursor writes async def endpoints with Pydantic response models, httpx AsyncClient for outbound calls, and typed SQLAlchemy sessions via Depends.

Customization tips

If you use Django or Flask, most rules do not apply — write separate rules. Teams that still run Pydantic v1 should update the Types section. For data pipelines, add a section about Pandas / Polars style.

Related resources

Frequently asked questions

Django compatible?

Mostly no. The rules are FastAPI-specific.

Requires Python 3.11+?

Recommended. Some async patterns assume 3.11 ergonomics.

Does it cover Celery?

Not by default. Add a Background Tasks section if needed.

mypy strict required?

Assumed on the services layer. Other layers can be lighter.

Works with SQLModel?

Mostly — edit the Database section to use SQLModel idioms.

Want more like this?

Browse the full RohanKit library — free resources for Claude and Cursor.

Back to RohanKit