Generate Pydantic Models for API Contracts
Skill for Pydantic multi-model API contracts: Base/Create/Update/Response/InDB pattern with camelCase alias support.
Why it matters
Streamline API development by generating robust Pydantic models that enforce clean contracts. This skill helps ensure data integrity and consistency across your backend services.
Outcomes
What it gets done
Generate Pydantic models using the multi-model pattern (Base, Create, Update, Response, InDB).
Implement camelCase aliases for seamless integration with frontend or other services.
Define optional fields for flexible update requests (PATCH).
Create database-specific models with `doc_type` for data persistence.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/ag-pydantic-models-py | bash Overview
Pydantic Models
A skill for generating Pydantic multi-model API contracts - Base/Create/Update/Response/InDB - with camelCase field aliasing for Cosmos DB-backed resources. Use when defining a new API resource's Pydantic models with clean create/update/response/database separation.
What it does
Pydantic Models creates Pydantic models following a multi-model pattern for clean API contracts, starting from a template (assets/template.py) with {{ResourceName}}/{{resource_name}} placeholders swapped for the actual PascalCase/snake_case resource name. The pattern splits a resource into five models: Base for fields shared across all variants, Create for the required-field request body on creation, Update with all fields optional for PATCH requests, Response carrying the full API response, and InDB adding a doc_type field for the database document.
When to use - and when NOT to
Use it when defining a new API resource's data models with clean separation between creation, update, and response shapes rather than one overloaded model. For frontend compatibility, fields support camelCase aliases while keeping snake_case internally:
class MyModel(BaseModel):
workspace_id: str = Field(..., alias="workspaceId")
created_at: datetime = Field(..., alias="createdAt")
class Config:
populate_by_name = True # Accept both snake_case and camelCase
Inputs and outputs
Update models make every field optional for PATCH semantics (Optional[str] = Field(None, min_length=1)), and the database-document model extends the response model with a doc_type string constant (e.g. "my_resource") used for Cosmos DB queries.
Integrations
Integration follows three steps: create the models in src/backend/app/models/, export them from src/backend/app/models/__init__.py, and add the corresponding TypeScript types on the frontend side to keep both sides of the API contract in sync.
Who it's for
Backend developers defining API resource models on a Pydantic/Cosmos DB stack who want a consistent Create/Update/Response/InDB split with camelCase-compatible field aliasing, rather than one general-purpose model reused across every request shape.
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.