marshmallow-polyfield
An unofficial extension to Marshmallow to allow for polymorphic fields
What it is and what it does
Marshmallow-Polyfield is an unofficial extension to Marshmallow that adds support for polymorphic fields—fields that can hold objects of different types and serialize/deserialize them appropriately. Instead of defining a single schema for a field, you provide two selector functions: one that examines an object during serialization and returns the correct schema, and another that examines incoming data during deserialization and returns the correct schema. This allows you to handle heterogeneous data structures where a single field might contain different object types that each need their own serialization logic.
The package integrates with Marshmallow's standard field properties like `required`, `allow_none`, and `many`, so polymorphic fields behave like any other Marshmallow field in your schema. It targets Marshmallow 3.0 and above and supports Python >= 3.5. The package has no external runtime dependencies beyond Marshmallow itself.
Use it for:
- Serialize REST API responses where a single field holds different object types.
- Build schemas for heterogeneous collections where each item's schema depends on its type.
- Handle polymorphic data in JSON APIs where type discrimination is based on attributes.
- Implement type-aware serialization in inheritance hierarchies with custom per-type handling.
- Deserialize incoming data by inspecting its structure to determine which schema to apply.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Extends Marshmallow with a PolyField class that handles serialization and deserialization of polymorphic types by selecting the appropriate schema based on the object's runtime type.
Yes, if you are already using Marshmallow 3.0+ and need polymorphic field support. The package is permissively licensed, has no external dependencies, and is stable for its intended use. However, it has been dormant since 2022-10-26 with no active maintenance—install only if you can tolerate potential incompatibilities with future Marshmallow releases.
Install
marshmallow-polyfield on PyPI
pip
pip install marshmallow-polyfielduv
uv add marshmallow-polyfieldpoetry
poetry add marshmallow-polyfieldInstalling marshmallow-polyfield
Before you install
High install friction; no runtime dependencies but the package has been dormant since 2022-10-26 (over 1388 days without release). Last commit was 2024-07-01. Suitable for projects already committed to Marshmallow 3.0+ that can tolerate lack of active maintenance.
License in practice
Licensed under Apache 2.0 (permissive), allowing commercial and private use with minimal restrictions. No license-based barriers to adoption.
Quickstart
pip install marshmallow-polyfield
from marshmallow_polyfield import PolyField
from marshmallow import Schema
class MySchema(Schema):
field = PolyField(
serialization_schema_selector=my_serializer,
deserialization_schema_selector=my_deserializer
)
Requires Marshmallow 3.0 or above; requires Python >= 3.5. You must define both serialization_schema_selector and deserialization_schema_selector functions.
Verify before relying
- Whether the package works correctly with recent Marshmallow versions given the long maintenance gap.
- Whether critical bugs would receive fixes under current maintenance status.
- Compatibility with Python versions beyond 3.7 (classifiers only list up to 3.7).
Package facts
| License | Apache 2.0 (permissive) |
| Python support | supports the current Python release (>=3.5) |
| Install friction | high — source build required |
| Runtime dependencies | none |
| Maintenance | dormant — 1,388 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 174,935/month — #10,275 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: marshmallow-polyfield-5.11.tar.gz
Keywords: serialization, rest, json, api, marshal, marshalling, deserialization, validation, schema
Tags
More Application Frameworks packages
FastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
textualTextual is a Python framework for building…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
mcpBuild and connect to Model Context Protocol…
permissive · top 1,000 on PyPI
WerkzeugWerkzeug is a WSGI utility library providing…
permissive · top 1,000 on PyPI
marshmallow-oneofschemaExtends marshmallow to handle polymorphic…
permissive · top 5,000 on PyPI
marshmallow-unionAdds union field support to marshmallow,…
permissive · top 5,000 on PyPI
marshmallow-fastoneofschemaExtends marshmallow to handle polymorphic…
permissive · top 15,000 on PyPI
marshmallowMarshmallow converts complex Python objects to…
permissive · top 1,000 on PyPI
marshmallow-enumProvides an Enum field type for Marshmallow…
permissive · top 5,000 on PyPI
apispec-oneofschemaExtends apispec to generate OpenAPI 3.0+…
permissive · top 15,000 on PyPI
marshmallow-mongoengineGenerates marshmallow schemas automatically…
permissive · top 15,000 on PyPI
flask-marshmallowFlask-Marshmallow integrates Flask and…
permissive · top 5,000 on PyPI
DeepFriedMarshmallowAccelerates Marshmallow schema serialization…
permissive · top 15,000 on PyPI
marshmallow-jsonapiProduces JSON API 1.0-compliant output from…
permissive · top 15,000 on PyPI