skillfed

marshmallow-polyfield

An unofficial extension to Marshmallow to allow for polymorphic fields

marshmallow-polyfield v5.11 174.9K downloads/30d#10,275 on PyPI59
Permissive license Apache 2.0 DORMANT released

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-polyfield

uv

uv add marshmallow-polyfield

poetry

poetry add marshmallow-polyfield

Installing 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

Intended Audience :: DevelopersLicense :: OSI Approved :: Apache Software LicenseProgramming Language :: Python :: 3Programming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: Implementation :: CPythonProgramming Language :: Python :: Implementation :: PyPy

Tags

polymorphic serialization marshmallowmarshmallow polymorphic fieldspolymorphic type handlingschema selection by typemarshmallow extension polymorphismdynamic schema selectionpolymorphic deserialization
serializationpolymorphismmarshmallow-extension

More Application Frameworks packages