Skip to main content

🐍🔗 SQLAlchemy-Pydantic Codegen

License PyPI Python Unit Tests

A Python library for generating Pydantic models from SQLAlchemy models, providing a seamless integration between SQLAlchemy and Pydantic for data validation and serialization.

✨ Key Features

  • 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.

📦 Installation

uv add sqlalchemy-pydantic-codegen

🚀 Usage

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)

🛠️ Custom Configuration

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

📤 Output

  • One Pydantic schema file per SQLAlchemy model.
  • init.py with all exports and forward references.
  • Cleaned and ready-to-use Pydantic models.

Metadata

Release files for sqlalchemy-pydantic-codegen 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlalchemy-pydantic-codegen 1.1.1
File Size Uploaded Provenance Actions
sqlalchemy_pydantic_codegen-1.1.1.tar.gz 18.9 kB Without provenance Details for sqlalchemy_pydantic_codegen-1.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sqlalchemy-pydantic-codegen 1.1.1
File Interpreter ABI Platform Actions
sqlalchemy_pydantic_codegen-1.1.1-py3-none-any.whl Python 3 none any Details for sqlalchemy_pydantic_codegen-1.1.1-py3-none-any.whl

Total release size: 35.5 kB

Release files / sqlalchemy_pydantic_codegen-1.1.1.tar.gz

Download URL sqlalchemy_pydantic_codegen-1.1.1.tar.gz
Size 18.9 kB
Tags Source
SHA-256 checksum
How to use checksums
cc3d907b785899be50725cc2cf26817f235289fcc3b3a95427b57297fda4a599
BLAKE2b-256 checksum
How to use checksums
9c037a1a0866bf159a61a3fb956623c5586c10b1cdbd6695fa13acc3bab905dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release files / sqlalchemy_pydantic_codegen-1.1.1-py3-none-any.whl

Download URL sqlalchemy_pydantic_codegen-1.1.1-py3-none-any.whl
Size 16.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c00b1d8b294bcba41433e0f17413fc35992aa98237e59af50a5808349b0ffb35
BLAKE2b-256 checksum
How to use checksums
794e80f672adeded98254796af5cc798042c11e59c6cd698b9f99f684fa64a83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Security for sqlalchemy-pydantic-codegen 1.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page