{"id":1544,"library":"marshmallow-dataclass","title":"marshmallow-dataclass","description":"marshmallow-dataclass is a Python library that enables the seamless conversion of standard Python dataclasses into Marshmallow schemas. This simplifies data serialization, deserialization, and validation by automatically generating schemas based on dataclass definitions. As of version 8.7.1, it provides robust type hint support and integrates well with existing Marshmallow workflows. Releases are generally driven by new features, bug fixes, or compatibility updates with Marshmallow.","status":"active","version":"8.7.1","language":"en","source_language":"en","source_url":"https://github.com/lovasoa/marshmallow_dataclass","tags":["dataclasses","marshmallow","serialization","deserialization","validation","type-hints"],"install":[{"cmd":"pip install marshmallow-dataclass","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for schema generation and data processing.","package":"marshmallow","optional":false},{"reason":"Required for the `dataclass_json` decorator functionality.","package":"dataclasses-json","optional":true}],"imports":[{"symbol":"class_schema","correct":"from marshmallow_dataclass import class_schema"},{"note":"The `dataclass_json` decorator was moved from the root module to `marshmallow_dataclass.decorators` in v8.0.0.","wrong":"from marshmallow_dataclass import dataclass_json","symbol":"dataclass_json","correct":"from marshmallow_dataclass.decorators import dataclass_json"}],"quickstart":{"code":"from dataclasses import dataclass, field\nfrom datetime import datetime\nfrom marshmallow_dataclass import class_schema\n\n@dataclass\nclass User:\n    id: int = field(metadata={'required': True})\n    name: str\n    email: str = field(metadata={'load_only': True})\n    created_at: datetime = field(default_factory=datetime.now, metadata={'dump_only': True})\n\n# Generate a Marshmallow schema from the dataclass\nUserSchema = class_schema(User)\n\n# Instantiate the schema\nuser_schema = UserSchema()\n\n# Example data\nuser_data = {\n    'id': 1,\n    'name': 'Alice',\n    'email': 'alice@example.com'\n}\n\n# Deserialize (load) data into a dataclass instance\ntry:\n    user_obj = user_schema.load(user_data)\n    print(f\"Loaded User: {user_obj.name} (ID: {user_obj.id})\")\n    # 'email' is load_only, so not in user_obj after load by default if not passed in constructor\n    print(f\"User email (load_only): {user_data.get('email')}\")\nexcept Exception as e:\n    print(f\"Error loading data: {e}\")\n\n# Serialize (dump) a dataclass instance to a dictionary\ndumped_data = user_schema.dump(user_obj)\nprint(f\"Dumped data: {dumped_data}\")\n# 'email' is load_only, 'created_at' is dump_only\nassert 'email' not in dumped_data\nassert 'created_at' in dumped_data\nassert dumped_data['id'] == 1\n","lang":"python","description":"This quickstart demonstrates how to define a dataclass, generate a Marshmallow schema using `class_schema`, and then use the generated schema to load (deserialize) data into a dataclass instance and dump (serialize) a dataclass instance back to a dictionary. It also shows how to use Marshmallow metadata fields like `required`, `load_only`, and `dump_only` within the dataclass `field` definition."},"warnings":[{"fix":"Use `marshmallow.fields.Union` directly instead of `union_field`. Import `dataclass_json` from `marshmallow_dataclass.decorators` if you still need it (though it's generally discouraged in favor of `class_schema`). Ensure your environment uses Python 3.8 or newer.","message":"Version 8.0.0 removed `union_field` and moved `dataclass_json`. It also bumped the minimum Python version requirement to 3.8+.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Always pass the dataclass directly to `class_schema(MyDataclass)` instead of `NewType('MySchema', MyDataclass)`. Review specific changes to `class_schema` parameters if you were using advanced configurations.","message":"Version 6.0.0 changed the signature of the `class_schema` function and discouraged the older `NewType` pattern for schema generation.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Define `post_load` or `pre_load` methods as part of the `UserSchema` class (if you're inheriting and extending it) or by dynamically adding them after schema generation if needed. Do not try to add these directly to your `dataclass` definition.","message":"Marshmallow `post_load` and `pre_load` methods must be defined on the *generated Marshmallow Schema*, not directly within the dataclass.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For an optional field that should *not* allow `None` during deserialization (e.g., if it must be missing or a valid value, but not `None`), explicitly set `field(metadata={'allow_none': False})`.","message":"Dataclass `Optional` types (`Optional[str]`) automatically map to Marshmallow fields with `allow_none=True`. If `None` is not desired for an optional field, you must explicitly set `allow_none=False` via metadata.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}