FastAPI + Python + PostgreSQL (Async SQLAlchemy & Pydantic v2)
Production guidelines for FastAPI, Pydantic v2 validation, Async SQLAlchemy 2.0 (asyncpg), Alembic migrations, and PostgreSQL/Redis.
- Formats
- 4 files
- AGENTS.md
- 63 lines
- CLAUDE.md
- 17 lines
- Languages
- Python
- Updated
- Sep 2026
- Used by
- 4 projects
Writes .claude/skills/fastapi-python-postgres/SKILL.md
$ curl -s --create-dirs -o .claude/skills/fastapi-python-postgres/SKILL.md https://stackitfast.com/rules/fastapi-python-postgres/SKILL.mdRule files
Works with Cursor · Claude Code · Windsurf · AGY
Architecture notes
Architecture Overview
Production conventions for high-throughput, async-first Python microservices and AI agent backends using FastAPI, SQLAlchemy 2.0, and PostgreSQL.
Verified Real-World Adoption
This architecture is deployed in production by AI and data platforms including Mem0, OpenBB, Baserow, and Open WebUI.
Key Architectural Nuances
- Async Connection Pooling: Leverages
asyncpgwithpool_pre_ping=Truefor resilient connection lifecycle management. - Strict Separation of DTOs: Pydantic v2 ensures fast serialization and automated OpenAPI documentation.
Frequently asked questions
Does this AGENTS.md work with Cursor, Claude Code, and Windsurf?
Yes — AGENTS.md is the open, cross-tool standard read by Cursor, Claude Code, Windsurf, and 30+ other agents. A dedicated .mdc file is also included for Cursor's native .cursor/rules format.
Why does async SQLAlchemy throw MissingGreenlet errors?
Lazy-loaded relationships trigger a synchronous database call under the hood, which async SQLAlchemy can't execute outside the greenlet context it uses to bridge sync and async code. Eager-loading the relation up front with selectinload() or joinedload() avoids the lazy trigger entirely, so there's no synchronous call left to fail.
Why use asyncpg instead of psycopg2 for FastAPI?
psycopg2 is a synchronous driver — using it inside an async def route would block the entire event loop on every query, defeating the purpose of async FastAPI. asyncpg is built for asyncio from the ground up and is also one of the fastest Postgres drivers available in Python.
Used in production
Explore all stacksMindsHub (formerly MindsDB)
1Splitting the client, API and agent into separately versioned git submodules lets each ship on its own cadence (a signed desktop app, a PyPI server package, an agent package), while one Makefile and one docker-compose.yml still bring the whole stack up together.
VoiceStudio
Heavy model work stays in a Python FastAPI backend with a pluggable, hardware-aware engine registry. Desktop, web, MCP and remote-worker clients all use the same local API, so voice models run on the user's own GPU without a cloud dependency.
LangChain
The current langchain package is a thin agent-building layer on top of LangGraph, with the older chain-based API kept separately as langchain-classic, and every model provider ships as its own versioned package so integrations can release independently of the core library.
FastAPI
FastAPI keeps its own dependency surface to Starlette and Pydantic and pushes servers, CLI, templating, forms and settings into an optional 'standard' extra, so a bare install stays minimal while typical apps opt into one bundle instead of assembling it dependency by dependency.