sqlalchemy-serializer
Mixin for SQLAlchemy models serialization without pain
What it is and what it does
SQLAlchemy-serializer is a mixin that adds a `.to_dict()` method to SQLAlchemy model instances, turning them into dictionaries suitable for JSON responses or data export. It handles nested relationships automatically, includes support for both traditional and modern SQLAlchemy 2.0 annotation styles, and offers fine-grained control over which fields to include or exclude via `rules`, `only`, and `exclude_values` parameters.
The package is designed for developers who need simple model-to-dict conversion without the overhead of schema libraries like marshmallow. It supports custom serialization logic per column, timezone handling via pytz, recursion depth limits to prevent infinite loops, and the ability to include non-SQLAlchemy fields or method results in the output. Runtime dependencies are SQLAlchemy, psycopg2-binary, pytz, and setuptools.
Use it for:
- Convert SQLAlchemy model instances to dictionaries for JSON API responses without defining separate serialization schemas.
- Exclude sensitive fields (passwords, tokens) from serialized output using negative rules or custom serializers.
- Serialize nested relationships and foreign-key references in a single call with configurable depth limits.
- Add computed or non-database fields to serialized output by including methods or properties in the rules.
- Filter out null or empty values from the serialized dictionary to reduce payload size in API responses.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Adds a `.to_dict()` method to SQLAlchemy model instances, converting them to dictionaries with support for nested relationships, custom field rules, and timezone handling.
Yes. The package is actively maintained, has no known vulnerabilities, uses a permissive MIT license, and solves a common problem—model serialization—with minimal dependencies and low install friction. It's well-suited for Flask/FastAPI applications and other projects where lightweight, ad-hoc model-to-dict conversion is needed. Install it if you want to avoid the boilerplate of schema libraries for straightforward serialization tasks.
Install
sqlalchemy-serializer on PyPI
pip
pip install sqlalchemy-serializeruv
uv add sqlalchemy-serializerpoetry
poetry add sqlalchemy-serializerInstalling sqlalchemy-serializer
Before you install
Low friction: pure Python wheel with four runtime dependencies (SQLAlchemy, psycopg2-binary, pytz, setuptools). Actively maintained with recent releases; last commit 2026-05-11 and 201 repository stars indicate steady upkeep.
License in practice
MIT license (permissive) allows use in commercial and proprietary projects with minimal restrictions—include a copy of the license and attribute the original author.
Quickstart
from sqlalchemy_serializer import SerializerMixin
class MyModel(db.Model, SerializerMixin):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(256))
item = MyModel.query.first()
result = item.to_dict()
Requires Python 3.10 or later.
Verify before relying
- Whether psycopg2-binary is required for all use cases or only when using PostgreSQL backends.
- Performance characteristics when serializing deeply nested relationships or large model graphs.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 4 — SQLAlchemy, psycopg2-binary, pytz, setuptools |
| Maintenance | actively maintained — 118 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 163,975/month — #10,558 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: sqlalchemy_serializer-1.6.3-py3-none-any.whl
Keywords: sqlalchemy, serialize, to_dict, JSON
Tags
More Database packages
psycopg2-binary is a PostgreSQL database…
copyleft · top 1,000 on PyPI
redisPython client library for connecting to and…
permissive · top 1,000 on PyPI
ydbYDB Python SDK is the official client library…
permissive · top 1,000 on PyPI
snowflake-connector-pythonConnects Python applications to Snowflake data…
permissive · top 1,000 on PyPI
sqlparsesqlparse tokenizes SQL text into a tree of…
permissive · top 1,000 on PyPI
dbt-adaptersProvides base adapter protocols and shared…
permissive · top 1,000 on PyPI
marshmallow-sqlalchemyGenerates marshmallow schemas from SQLAlchemy…
permissive · top 5,000 on PyPI
sqlalchemy-jsonProvides mutation-tracked JSON column types for…
permissive · top 5,000 on PyPI
sqlalchemy-easy-softdeleteAdds soft-deletion support to SQLAlchemy models…
permissive · top 15,000 on PyPI
sqlalchemy-mixinsProvides Active Record-style mixins for…
permissive · top 15,000 on PyPI
drf-writable-nestedExtends Django REST Framework serializers to…
permissive · top 15,000 on PyPI
voluptuous-serializeConverts Voluptuous schema objects into…
permissive · top 15,000 on PyPI
marshmallow-jsonapiProduces JSON API 1.0-compliant output from…
permissive · top 15,000 on PyPI
sqlalchemy-reprAdds automatic, human-readable repr() output to…
unclear · top 15,000 on PyPI
sqlacodegenReads an existing database schema and generates…
permissive · top 5,000 on PyPI
drf-jsonschema-serializerConverts Django REST Framework serializers to…
permissive · top 15,000 on PyPI