Cosmic FastAPI v2.0.0
Cosmic FastAPI v2.0.0 turns the project into a reusable, agent-friendly FastAPI standard inspired by Architecture Patterns with Python. It preserves the book's domain-first dependency direction while adopting FastAPI, Pydantic 2, SQLAlchemy 2, Alembic, uv, Ruff, Pyrefly, and pytest.
This is a major release because the template structure and extension workflow changed substantially. New projects receive explicit architecture guidance, a modern persistence baseline, CQRS read paths, and a tested user-registration example that demonstrates how the pieces fit together.
Highlights
- Added a complete modern Cosmic Python baseline: aggregate roots, repositories, unit of work, command handlers, domain events, message bus, composition root, and CQRS read models.
- Added SQLAlchemy 2 persistence adapters and Alembic migrations.
- Added immutable Pydantic command and event schemas with idiomatic
snake_casePython fields andcamelCaseJSON contracts for HTTP, CLI, and future message-queue adapters. - Added ADR-driven agent guidance with compact decision pruning through
make adr-contextand validation throughmake adr-check. - Added a Cosmic Python coverage matrix that distinguishes built-in patterns from conditional extensions such as optimistic locking, transactional outbox, broker adapters, idempotency storage, and sagas.
- Hardened registration consistency: timezone-aware timestamps and race-safe duplicate-email handling preserve the HTTP
409contract.
Breaking Changes
-
Domain API schemas moved from
src/template/domain/schemas.pytosrc/template/entrypoint/schemas.py. -
Monitor response models moved out of domain events into entrypoint schemas.
-
New relational persistence requires applying Alembic migrations before normal local startup:
make migrate
-
SQLAlchemy and Alembic are now runtime dependencies.
-
The redundant local
template.utils.formatting.to_camelhelper was removed. Importpydantic.alias_generators.to_cameldirectly.
Added
Modern Cosmic Python Architecture
- Plain-Python
Useraggregate root andUserSettingsvalue object. - Frozen Pydantic
Command,Event, andMessagebases. RegisterUsercommand andUserRegistereddomain event.- Generic in-process message bus with command dispatch, domain-event draining, and explicit unsupported-message rejection.
- Application handlers, repository port, abstract unit of work, and explicit bootstrap composition root.
- Thin FastAPI registration and lookup entrypoints.
Persistence and CQRS
- SQLAlchemy 2 typed declarative records under
adapters. - SQLAlchemy repository and unit-of-work adapters.
- Alembic configuration and initial users-table migration.
- Database settings with migrate-first defaults and opt-in schema creation for isolated demos and tests.
- Purpose-built CQRS
UserReadModel, reader port, and SQLAlchemy projection adapter so read-only endpoints do not rehydrate write aggregates by habit.
Architecture Guidance for Agents
- Root
AGENTS.mdwith Python style, GIVEN/WHEN/THEN testing conventions, architecture workflow, and Conventional Commits guidance. - ADR registry and lifecycle validation.
scripts/prune_decisions.pyplusmake adr-contextandmake adr-check.- ADRs covering modern Cosmic Python defaults, framework boundaries, SQLAlchemy adapters, explicit composition, async policy, tooling, Pyrefly, Conventional Commits, ADR lifecycle, Pydantic messages, camel-case JSON contracts, aggregate boundaries, and CQRS reads.
docs/cosmic-python-coverage.mdpattern coverage matrix.
Tooling and Test Coverage
- Pyrefly static type checking.
- Expanded Ruff checks and formatting across source, tests, scripts, and migrations.
- Updated pre-commit Ruff hooks.
- Migration, persistence, unit-of-work, aggregate, command-schema, message-bus, CQRS, bootstrap, and FastAPI end-to-end tests.
- 100% application coverage gate.
Changed
- Refreshed README branding and terminology.
- Documented database environment variables and migrate-first local startup.
- Standardized JSON schema serialization on
camelCaseusing Pydantic's built-in alias generator. - Added local ignores for IDE metadata and generated reports.
- Normalized
.dockerignoreformatting.
Fixed
- Configured
created_atas timezone-aware in both SQLAlchemy metadata and the Alembic migration. - Moved transport-agnostic registration validation into the command schema so HTTP, CLI, and broker callers share the same contract.
- Added nested command-settings serialization with camel-case JSON aliases.
- Translated SQLAlchemy
IntegrityErrorinto an application-level conflict so concurrent duplicate registrations continue to return HTTP409. - Updated the Uvicorn host default to satisfy static typing.
Removed
- Removed domain-layer API response schemas.
- Removed monitor schemas from domain events.
- Removed the local camel-case formatting helper and its redundant tests.
Validation
uv run pre-commit run --all-filesmake lintmake covermake adr-checkgit diff --check
Application coverage remains at 100%.
Full Commit Changelog
a98229adocs: update README with aligned branding and terminology changes592d97bdocs: update README with aligned branding and terminology changes67d2cadMerge remote-tracking branchorigin/developintodevelop61a6ab2Merge pull request #3 fromtomasanchez/develop22c6561chore: run Ruff format02c20d5chore(Makefile): update Ruff tasks for format and lint commands740c1eafeat(architecture): add cosmic agent standard and ADR pruner630db51chore(tooling): refresh hooks and local ignores3c0c981feat(architecture): complete modern cosmic patternscd9370ddocs(architecture): define cosmic agent standard54530b1chore: normalize dockerignore newlinec8e0db9refactor(formatting): remove redundant camel case helper6740a5dfix(architecture): address registration consistency review084a287Merge pull request #5 fromtomasanchez/develop
Links
- Full diff: v1.0.0...v2.0.0
- Architecture PR: #5
- Cosmic Python: https://www.cosmicpython.com/book/preface.html