Architecture Blueprint
Workflow
- •Requirement Distillation: Deconstruct high-level features into granular data models and business logic flows.
- •Schema-First Design: Define Pydantic V2 schemas for I/O validation and SQLAlchemy 2.0 models for the persistent domain layer.
- •Dependency Injection (DI) Orchestration: Implement
Dependsfor modular service provision, focusing on asynchronous database session management. - •Service Layer Implementation: Encapsulate business rules in standalone services, ensuring the API layer (Routes) remains a thin orchestration shell.
- •Robust Error Handling: Deploy global exception middleware to maintain a consistent API response contract ( lookup for error codes).
Constraints & Standards
- •Full Async Chain: Every I/O operation must be non-blocking. Use
awaitfor DB queries and external API calls. - •Atomic Transactions: Ensure data integrity via the "Unit of Work" pattern. Use context managers for session commits and rollbacks.
- •Zero N+1 Leakage: Explicitly use
selectinloadorjoinedloadfor relationship loading to optimize database roundtrips. - •Security & Auth: Implement JWT-based authentication with OAuth2PasswordBearer. Enforce strict Pydantic
response_modelto prevent PII (Personally Identifiable Information) leakage. - •Code Quality: Adhere to PEP 8, utilize Type Hinting for all parameters, and maintain an or better complexity for data processing logic.
Technical Specification Template
- •Database: SQLAlchemy 2.0 (Declarative Mapping + Async Engine).
- •Migration: Mandatory Alembic versioning for all schema changes.
- •Validation: Pydantic V2 with strict type coercion.
- •API Documentation: Auto-generated OpenAPI (Swagger) with comprehensive status code definitions (200, 201, 400, 401, 403, 404, 500).
Self-Reflective Audit
- •Before finalizing any module, verify:
- •Is the business logic strictly decoupled from the FastAPI router?
- •Are the database queries optimized for the expected scale?
- •Does the error handling prevent stack trace exposure to the end-user?