A Python library for generating Pydantic models from SQLAlchemy models, providing a seamless integration between SQLAlchemy and Pydantic for data validation and serialization.
- Automatic Pydantic model generation from SQLAlchemy models.
- Relationship support: Nested models for SQLAlchemy relationships.
- Custom JSON/JSONB field mapping to your own Pydantic models.
- Auto-generated
__init__.py
for schema packages.
uv add sqlalchemy-pydantic-codegen
We recommend sqlacodegen to generate your SQLAlchemy models automatically.
Once your SQLAlchemy models are ready, generate Pydantic models with:
sqlalchemy-pydantic-codegen --models-path my_app.db.models --output-dir src/schemas
--models-path
: Dotted path to your SQLAlchemy models (required)--output-dir
: Output directory for generated schemas (default:src/schemas
)
To map JSON/JSONB fields to custom Pydantic models, use the --config option.
Create a config file (e.g., codegen_config.py
):
# codegen_config.py
# Maps table names to a dictionary of field names and the Pydantic model to use.
CUSTOM_JSONB_MODELS = {
"my_table": {
"my_jsonb_field": "MyCustomPydanticModelForJsonbField",
},
}
# Maps the Pydantic model name to its full import statement.
CUSTOM_IMPORTS = {
"MyCustomPydanticModelForJsonbField": "from my_app.schemas import MyCustomPydanticModelForJsonbField",
}
Then, run the command with the --config
flag:
sqlalchemy-pydantic-codegen --models-path my_app.db.models --output-dir src/schemas --config codegen_config.py
- One Pydantic schema file per SQLAlchemy model.
- init.py with all exports and forward references.
- Cleaned and ready-to-use Pydantic models.