Skip to main content

Python library to convert dataclasses into marshmallow schemas.

Project description


Build Status

Automatic generation of marshmallow schemas from dataclasses.

How to use

You simply import marshmallow_dataclass.dataclass instead of dataclasses.dataclass. It adds a Schema property to the generated class, containing a marshmallow Schema class.

If you need to specify custom properties on your marshmallow fields (such as attribute, error, validate, required, dump_only, error_messages, description ...) you can add them using the metadata argument of the field function.

from dataclasses import field
from marshmallow_dataclass import dataclass # Importing from marshmallow_dataclass instead of dataclasses
import marshmallow.validate
from typing import List, Optional

class Building:
  # The field metadata is used to instantiate the marshmallow field
  height: float = field(metadata={'validate': marshmallow.validate.Range(min=0)})
  name: str = field(default="anonymous")

class City:
  name: Optional[str]
  buildings: List[Building] = field(default_factory=lambda: [])

# City.Schema contains a marshmallow schema class
city, _ = City.Schema().load({
    "name": "Paris",
    "buildings": [
        {"name": "Eiffel Tower", "height":324}

# Serializing city as a json string
city_json, _ = City.Schema().dumps(city)

The previous syntax is very convenient, as the only change you have to apply to your existing code is update the dataclass import.

However, as the .Schema property is added dynamically, it can confuse type checkers. If you want to avoid that, you can also use the standard dataclass decorator, and generate the schema manually using class_schema :

from dataclasses import dataclass
from datetime import datetime
import marshmallow_dataclass

class Person:
    name: str
    birth: datetime

PersonSchema = marshmallow_dataclass.class_schema(Person)

You can also declare the schema as a ClassVar:

from marshmallow_dataclass import dataclass
from marshmallow import Schema
from typing import ClassVar, Type

class Point:
  Schema: ClassVar[Type[Schema]] = Schema

You can specify the Meta just as you would in a marshmallow Schema:

from marshmallow_dataclass import dataclass

class Point:
  class Meta:
    ordered = True


This package is hosted on pypi :

pipenv install marshmallow-dataclass


The project documentation is hosted on github pages:

Usage warning

This library depends on python's standard typing library, which is provisional.

Project details

Download files

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

Files for marshmallow-dataclass, version 6.0.0b1
Filename, size File type Python version Upload date Hashes
Filename, size marshmallow_dataclass-6.0.0b1-py3-none-any.whl (7.0 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size marshmallow_dataclass-6.0.0b1.tar.gz (5.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page