fastapi-patterns

Implement FastAPI service patterns with Pydantic v2 DTOs and repositories.

3|Updated Mar 1, 2010
One-click install
npx skills add https://github.com/harleypig/dotfiles --skill fastapi-patterns-harleypig
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: fastapi-patterns
Source: https://github.com/harleypig/dotfiles/tree/main/config/claude/skills/fastapi-patterns
Command: npx skills add https://github.com/harleypig/dotfiles --skill fastapi-patterns-harleypig

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Concrete, copy-adaptable recipes for FastAPI services with Pydantic v2 and SQLAlchemy 2.0. The conventional guidelines in rules/fastapi.md establish the baseline; this skill adds deeper, idiom-specific patterns for DTOs, repositories, and error handling to speed up design and implementation.

Core Features & Use Cases

  • DTOs (Pydantic v2): BaseDTO with ConfigDict, model_config flags for from_attributes, populate_by_name, and whitespace stripping.
  • Create / Read / Update split: separate DTOs for create, read, and update semantics with per-field controls and patch semantics.
  • Validation and computed fields: field_validator and model_validator usage; @computed_field to derive values at serialization.
  • Repository pattern and endpoints wiring: generic BaseRepository with session injection via Depends; example domain-specific repos with eager loading strategies.
  • Exception mapping: central AppError hierarchy and a single HTTP exception handler to decouple business logic from HTTP concerns.

Quick Start

Apply these patterns by creating DTOs, wiring a per-request session dependency, implementing a repository layer, and mapping AppError to HTTP responses in your FastAPI app.

Frequently Asked Questions about fastapi-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure Pydantic v2 DTOs for FastAPI create, read, and update operations?

FastAPI DTO design uses a Create, Read, and Update split with separate Pydantic v2 models. A BaseDTO with ConfigDict controls from_attributes, populate_by_name, and whitespace stripping, while per-field controls manage patch semantics.

How do I implement a repository pattern with FastAPI dependency injection?

FastAPI repository pattern implementation uses a generic BaseRepository with SQLAlchemy session injection via Depends. Domain-specific repositories extend this base layer to handle data access and apply eager loading strategies.

What is the best way to map custom business logic exceptions to HTTP errors in FastAPI?

FastAPI exception mapping uses a central AppError hierarchy and a single HTTP exception handler. This decouples business logic from HTTP concerns by translating AppError instances into appropriate HTTP responses.

How do computed fields and validators work in FastAPI Pydantic v2 models?

Pydantic v2 validation uses field_validator and model_validator for input checks, while the @computed_field decorator derives values dynamically during serialization. This ensures data integrity and calculated outputs in FastAPI DTOs.

Can I use SQLAlchemy 2.0 session injection with FastAPI repository layers?

FastAPI repository layers support SQLAlchemy 2.0 session injection through dependency wiring. A per-request session dependency connects the database session to the repository, ensuring scoped database access for each endpoint call.

When should I separate DTOs instead of using a single Pydantic model for FastAPI endpoints?

Separating DTOs is necessary when create, read, and update operations require distinct field controls or patch semantics. Using a single Pydantic model risks exposing internal fields or accepting invalid inputs during FastAPI data transfer.