# Python & FastAPI Best Practices (2026): uv, Ruff & Agent Skills

- **Topic**: Python & FastAPI
- **Canonical URL**: https://builderthread.dev/skill/for/python
- **Parent Hub**: https://builderthread.dev/skill.md
- **Last Verified**: 2026-11-21

Modern Python backends rely on uv lockfiles, Ruff linting, Pydantic v2 models, and non-blocking async FastAPI handlers. Use this checklist for AI services and APIs, or inspect the official FastAPI, Astral, and Qdrant SKILL.md repositories linked below.

---

## Core Python & FastAPI Best Practices Checklist

### 1. Use uv and pyproject.toml instead of global pip and requirements.txt

Pin reproducible dependencies with uv.lock and pyproject.toml so local dev, CI, and container builds resolve identical package graphs in seconds.

- **Avoid**: `pip install fastapi uvicorn && pip freeze > requirements.txt`
- **Prefer**: `uv init && uv add fastapi pydantic-settings && uv run pytest`

### 2. Keep blocking I/O out of async def FastAPI routes

Use async drivers (httpx, asyncpg) inside async def handlers, or declare regular def routes so FastAPI offloads blocking calls to its threadpool.

- **Avoid**: `async def fetch(): return requests.get(url).json()`
- **Prefer**: `async def fetch(client: httpx.AsyncClient): return (await client.get(url)).json()`

### 3. Validate config and request payloads with Pydantic v2 models

Define strict BaseModel schemas for request/response contracts and pydantic-settings for environment variables so bad config fails at startup.

- **Avoid**: `api_key = os.getenv("OPENAI_KEY"); payload = await request.json()`
- **Prefer**: `class Settings(BaseSettings): openai_key: SecretStr`

### 4. Inject database sessions and auth via Depends, not global singletons

Use FastAPI dependency injection with yield lifecycles so tests override dependencies cleanly and sessions close after every request.

- **Avoid**: `db = SessionLocal()  # module-level shared session`
- **Prefer**: `def get_db(): with SessionLocal() as db: yield db`

### 5. Type-annotate public functions and check with ty or mypy

Add Python 3.12+ native type hints (list[str], str | None) and run static type checking alongside Ruff so contract bugs fail before deploy.

- **Avoid**: `def summarize(items, limit=None):`
- **Prefer**: `def summarize(items: Sequence[Document], limit: int | None = None) -> Summary:`

### 6. Gate changes with Ruff format, Ruff check, and pytest

Run fast lint, format, type, and test gates after every coding agent edit so broken imports and regressions never land.

- **Avoid**: `python main.py (manual smoke test)`
- **Prefer**: `uv run ruff check . && uv run ruff format --check . && uv run pytest`

---

## Verified Skills for Python & FastAPI (8)

| Skill | Publisher | License | Stars | GitHub Source | Install Command |
|---|---|---|---|---|---|
| [fastapi](https://builderthread.dev/skill/fastapi/fastapi/fastapi.md) | FastAPI | MIT | 104,995 | [GitHub](https://github.com/fastapi/fastapi) | `npx skills add fastapi/fastapi --skill fastapi` |
| [pytest-skill](https://builderthread.dev/skill/LambdaTest/agent-skills/pytest-skill.md) | TestMu AI (LambdaTest) | MIT | 393 | [GitHub](https://github.com/LambdaTest/agent-skills/tree/main/pytest-skill) | `npx skills add LambdaTest/agent-skills --skill pytest-skill` |
| [uv](https://builderthread.dev/skill/astral-sh/agent-plugins/uv.md) | Astral | Apache-2.0 | 321 | [GitHub](https://github.com/astral-sh/agent-plugins/tree/main/plugins/astral/skills/uv) | `npx skills add astral-sh/agent-plugins --skill uv` |
| [ruff](https://builderthread.dev/skill/astral-sh/agent-plugins/ruff.md) | Astral | Apache-2.0 | 321 | [GitHub](https://github.com/astral-sh/agent-plugins/tree/main/plugins/astral/skills/ruff) | `npx skills add astral-sh/agent-plugins --skill ruff` |
| [ty](https://builderthread.dev/skill/astral-sh/agent-plugins/ty.md) | Astral | Apache-2.0 | 321 | [GitHub](https://github.com/astral-sh/agent-plugins/tree/main/plugins/astral/skills/ty) | `npx skills add astral-sh/agent-plugins --skill ty` |
| [qdrant-clients-sdk](https://builderthread.dev/skill/qdrant/skills/qdrant-clients-sdk.md) | Qdrant | Apache-2.0 | 271 | [GitHub](https://github.com/qdrant/skills/tree/main/skills/qdrant-clients-sdk) | `npx skills add qdrant/skills --skill qdrant-clients-sdk` |
| [designing-python-apis](https://builderthread.dev/skill/wdm0006/python-skills/designing-python-apis.md) | Will McGinnis | MIT | 117 | [GitHub](https://github.com/wdm0006/python-skills/tree/main/skills/python/api-design) | `npx skills add wdm0006/python-skills --skill designing-python-apis` |
| [py-refactor](https://builderthread.dev/skill/l-mb/python-refactoring-skills/py-refactor.md) | l-mb (Community) | MIT | 58 | [GitHub](https://github.com/l-mb/python-refactoring-skills/tree/main/skills/py-refactor) | `npx skills add l-mb/python-refactoring-skills --skill py-refactor` |

---

## Frequently Asked Questions

### What is the recommended modern Python toolchain for AI coding agents in 2026?

Use uv for project and dependency management, Ruff for linting and formatting, ty or mypy for type checking, Pydantic v2 for validation, FastAPI for async APIs, and pytest for automated tests.

### Which official Python SKILL.md repositories exist on GitHub?

FastAPI publishes an official skill in fastapi/fastapi, Astral publishes official skills for uv, ruff, and ty in astral-sh/agent-plugins, and Qdrant publishes vector-search client skills in qdrant/skills.

### Why do async FastAPI apps stall when written by generic coding agents?

Generic prompts often mix synchronous libraries like requests or psycopg2 inside async def routes, blocking the ASGI event loop. FastAPI and Python API skills enforce async clients or sync route definitions with dependency injection.

---

## Related Skill Guides

- [Database & ORM](https://builderthread.dev/skill/for/database.md) ([Web](https://builderthread.dev/skill/for/database)): Best Agent Skills for Postgres, Neon, Prisma & Drizzle ORM
- [Security](https://builderthread.dev/skill/for/security.md) ([Web](https://builderthread.dev/skill/for/security)): Best Agent Skills for Application Security & Audits
- [Testing](https://builderthread.dev/skill/for/testing.md) ([Web](https://builderthread.dev/skill/for/testing)): Best Agent Skills for Automated Testing & Playwright CLI
- [Architecture & Tools](https://builderthread.dev/skill/for/architecture.md) ([Web](https://builderthread.dev/skill/for/architecture)): Best Agent Skills for Architecture, Cloudflare & Simplicity

---

## Ship With a Private Starter Repo, Living PRD & MCP Tasks

Builderthread generates a private Python FastAPI repo with hashed API keys, streaming endpoints, Alembic migrations, pytest, living PRD, and MCP build tasks.

- Supported languages: Rust, TypeScript, React Native (Expo), Python
- Starter stacks: Next.js SaaS, Hono API, FastAPI AI, Expo mobile, Tauri Rust desktop, WXT Chrome extension
- Start now: https://builderthread.dev/auth/signup
