Skip to main content
Warning: You are using the test version of PyPI. This is a pre-production deployment of Warehouse. Changes made here affect the production instance of TestPyPI (
Help us improve Python packaging - Donate today!

Marshmallow multiplexing schema

Project Description


.. image::
:alt: Build Status

An extenstion to Marshmallow to support schema (de)multiplexing.

Marshmallow is a fantastic library for serialization and deserialization of data.
For more on that project see its `GitHub <>`_
page or its `Documentation <>`_.

This library adds a special kind of schema that actually multiplexes other schemas
based on object type. When serializing values, it uses get_obj_type() method
to get object type name. Then it uses `type_schemas` name-to-Schema mapping
to get schema for that particular object type, serializes object using that
schema and adds an extra field with name of object type. Deserialization is reverse.


$ pip install marshmallow-oneofschema

Here is how to import the necessary field class

from marshmallow_oneofschema import OneOfSchema


The code below demonstrates how to setup a schema with a PolyField. For the full context check out the tests.
Once setup the schema should act like any other schema. If it does not then please file an Issue.

.. code:: python

import marshmallow
import marshmallow.fields
from marshmallow_oneofschema import OneOfSchema

class Foo(object):
def __init__(self, foo): = foo

class Bar(object):
def __init__(self, bar): = bar

class FooSchema(marshmallow.Schema):
foo = marshmallow.fields.String(required=True)

def make_foo(self, data):
return Foo(**data)

class BarSchema(marshmallow.Schema):
bar = marshmallow.fields.Integer(required=True)

def make_bar(self, data):
return Bar(**data)

class MyUberSchema(OneOfSchema):
type_schemas = {
'foo': FooSchema,
'bar': BarSchema,

def get_obj_type(self, obj):
if isinstance(obj, Foo):
return 'foo'
elif isinstance(obj, Bar):
return 'bar'
raise Exception('Unknown object type: %s' % obj.__class__.__name__)

MyUberSchema().dump([Foo(foo='hello'), Bar(bar=123)], many=True).data
# => [{'type': 'foo', 'foo': 'hello'}, {'type': 'bar', 'bar': 123}]

MyUberSchema().load([{'type': 'foo', 'foo': 'hello'}, {'type': 'bar', 'bar': 123}]).data
# => [Foo('hello'), Bar(123)]

By default get_obj_type() returns obj.__class__.__name__, so you can just reuse that
to save some typing:

.. code:: python

class MyUberSchema(OneOfSchema):
type_schemas = {
'Foo': FooSchema,
'Bar': BarSchema,

You can customize type field with `type_field` class property:

.. code:: python

class MyUberSchema(OneOfSchema):
type_field = 'object_type
type_schemas = {
'Foo': FooSchema,
'Bar': BarSchema,

MyUberSchema().dump([Foo(foo='hello'), Bar(bar=123)], many=True).data
# => [{'object_type': 'Foo', 'foo': 'hello'}, {'object_type': 'Bar', 'bar': 123}]

You can use resulting schema everywhere marshmallow.Schema can be used, e.g.

.. code:: python
import marshmallow as m
import marshmallow.fields as f

class MyOtherSchema(m.Schema):
items = f.List(f.Nested(MyUberSchema))

Release History

This version
History Node


Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Hash SHA256 Hash Help Version File Type Upload Date
(4.4 kB) Copy SHA256 Hash SHA256
Source Mar 15, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting