Generate Pydantic models from SQLAlchemy models for data validation and serialization.
Pydantic-SQLAlchemy is a Python library that automatically generates Pydantic models from existing SQLAlchemy models. It bridges the gap between database layer definitions and API data validation, enabling seamless use of SQLAlchemy models in Pydantic-based applications like FastAPI. The project is described as experimental, with the author now recommending SQLModel for a more robust solution.
Python developers working with SQLAlchemy for database modeling and Pydantic for data validation, particularly those building APIs with frameworks like FastAPI who need to avoid duplicating model definitions.
It eliminates the need to manually maintain separate Pydantic schemas by dynamically generating them from SQLAlchemy models, reducing boilerplate and ensuring consistency. However, the author notes limitations like lack of autocompletion and recommends SQLModel for most use cases as a more integrated alternative.
Tools to convert SQLAlchemy models to Pydantic models
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Dynamically creates Pydantic models from SQLAlchemy declarative classes, eliminating manual schema duplication and reducing boilerplate, as shown in the quick example with User and Address models.
Handles SQLAlchemy relationships to allow nested Pydantic models for related data, enabling seamless API responses with nested structures, demonstrated in the example with addresses in the User model.
Supports excluding specific columns and overriding Pydantic configuration, providing flexibility in model generation, as noted in the release notes for versions 0.0.3 and 0.0.4.
Works with SQLAlchemy extended types like UtcDateTime, ensuring accurate type mapping for specialized database columns, as mentioned in the release notes for version 0.0.6.
The README admits that dynamically generated models lack autocompletion and inline errors, which hampers developer productivity and type safety compared to static Pydantic models.
The project is labeled as experimental with a clear warning to use SQLModel instead, indicating limited future support, reduced robustness, and potential abandonment in favor of the recommended alternative.
Since models are generated at runtime, they may not integrate well with advanced Pydantic features or tooling, leading to debugging challenges and reduced code maintainability for complex schemas.