Tactical blueprint for production-grade FastAPI applications. Focuses on procedural execution, tool-calling sequences, and idiomatic best practices.
This blueprint provides the procedural truth for engineering, testing, and deploying high-fidelity Python backends in the Antigravity Agent Factory.
Follow these procedures to implement the capability:
FastAPI applications in this factory follow a Domain-Driven Design (DDD) structure. Execute these steps to add a new domain:
mkdir -p src/domains/[domain_name]/{models,schemas,routers,services}.models.py using SQLAlchemy 2.0 Mapped syntax.In, Out, and Update.src/main.py with versioned prefix (e.g., /api/v1/[domain]).get_async_session dependency.select() and scalar_one_or_none() for single items; scalars().all() for lists.selectinload() for relationships to prevent "Truth" violations (Async N+1 errors).HTTPBearer or OAuth2PasswordBearer for all protected routes.Exception handler that returns standard JSON error responses.@field_validator for complex business logic validation.| Symptom | Probable Cause | Recovery Operation |
| :--- | :--- | :--- |
| MissingGreenlet | Synchronous operation in async session. | Ensure all DB calls are awaited and use AsyncSession; check for run_sync() usage for legacy sync operations. |
| ValidationError | Pydantic model mismatch. | Audit the response_model in the route decorator; verify all fields match the DB model or DTO. |
| 422 Unprocessable Entity | Data type mismatch or missing field. | Check the OpenAPI docs (/docs) for specific field errors; ensure Optional fields have defaults. |
class UserCreate(BaseModel):
email: EmailStr
password: str = Field(..., min_length=8)
@field_validator("password")
def password_complexity(cls, v: str) -> str:
if not any(c.isdigit() for c in v):
raise ValueError("Password must contain at least one digit")
return v
@router.post("/", response_model=UserOut, status_code=status.HTTP_211_CREATED)
async def create_user(
user_in: UserCreate,
db: AsyncSession = Depends(get_async_session),
current_user: User = Depends(get_current_active_user)
):
"""
Observable creation flow with structured logging and audit trailing.
"""
logger.info("creating_user", user_email=user_in.email, triggered_by=current_user.id)
return await UserService(db).create(user_in)
| Action | Command |
| :--- | :--- |
| Run Dev Server | fastapi dev src/main.py |
| Fast Test Loop | pytest -v -k "not integration" |
| Run Migrations | alembic upgrade head |
| Format Code | ruff format . |
Use this blueprint whenever building, refactoring, or debugging a Python/FastAPI service. It is the authoritative source for "How we build" vs "What FastAPI is."
Category:other