Skip to main content

cattrs: Flexible Object Serialization and Validation

Because validation belongs to the edges.

Documentation License: MIT PyPI Supported Python Versions Downloads Coverage


cattrs is a Swiss Army knife for (un)structuring and validating data in Python. In practice, that means it converts unstructured dictionaries into proper classes and back, while validating their contents.

Example

cattrs works best with attrs classes, and dataclasses where simple (un-)structuring works out of the box, even for nested data, without polluting your data model with serialization details:

>>> from attrs import define
>>> from cattrs import structure, unstructure
>>> @define
... class C:
...     a: int
...     b: list[str]
>>> instance = structure({'a': 1, 'b': ['x', 'y']}, C)
>>> instance
C(a=1, b=['x', 'y'])
>>> unstructure(instance)
{'a': 1, 'b': ['x', 'y']}

Have a look at Why cattrs? for more examples!

Features

Recursive Unstructuring

  • attrs classes and dataclasses are converted into dictionaries in a way similar to attrs.asdict(), or into tuples in a way similar to attrs.astuple().
  • Enumeration instances are converted to their values.
  • Other types are let through without conversion. This includes types such as integers, dictionaries, lists and instances of non-attrs classes.
  • Custom converters for any type can be registered using register_unstructure_hook.

Recursive Structuring

Converts unstructured data into structured data, recursively, according to your specification given as a type. The following types are supported:

  • typing.Optional[T] and its 3.10+ form, T | None.
  • list[T], typing.List[T], typing.MutableSequence[T], typing.Sequence[T] convert to lists.
  • tuple and typing.Tuple (both variants, tuple[T, ...] and tuple[X, Y, Z]).
  • set[T], typing.MutableSet[T], and typing.Set[T] convert to sets.
  • frozenset[T], and typing.FrozenSet[T] convert to frozensets.
  • dict[K, V], typing.Dict[K, V], typing.MutableMapping[K, V], and typing.Mapping[K, V] convert to dictionaries.
  • typing.TypedDict, both ordinary and generic.
  • typing.NewType
  • PEP 695 type aliases on 3.12+
  • attrs classes with simple attributes and the usual __init__1.
  • All attrs classes and dataclasses with the usual __init__, if their complex attributes have type metadata.
  • Unions of supported attrs classes, given that all of the classes have a unique field.
  • Unions of anything, if you provide a disambiguation function for it.
  • Custom converters for any type can be registered using register_structure_hook.

Batteries Included

cattrs comes with pre-configured converters for a number of serialization libraries, including JSON (standard library, orjson, UltraJSON), msgpack, cbor2, bson, PyYAML, tomlkit and msgspec (supports only JSON at this time).

For details, see the cattrs.preconf package.

Design Decisions

cattrs is based on a few fundamental design decisions:

  • Un/structuring rules are separate from the models. This allows models to have a one-to-many relationship with un/structuring rules, and to create un/structuring rules for models which you do not own and you cannot change. (cattrs can be configured to use un/structuring rules from models using the use_class_methods strategy.)
  • Invent as little as possible; reuse existing ordinary Python instead. For example, cattrs did not have a custom exception type to group exceptions until the sanctioned Python exceptiongroups. A side-effect of this design decision is that, in a lot of cases, when you're solving cattrs problems you're actually learning Python instead of learning cattrs.
  • Resist the temptation to guess. If there are two ways of solving a problem, cattrs should refuse to guess and let the user configure it themselves.

A foolish consistency is the hobgoblin of little minds, so these decisions can and are sometimes broken, but they have proven to be a good foundation.

Credits

Major credits to Hynek Schlawack for creating attrs and its predecessor, characteristic.

cattrs is tested with Hypothesis, by David R. MacIver.

cattrs is benchmarked using perf and pytest-benchmark.

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

  1. Simple attributes are attributes that can be assigned unstructured data, like numbers, strings, and collections of unstructured data.

Release files for cattrs 26.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cattrs 26.2.0
File Size Uploaded
cattrs-26.2.0.tar.gz 525.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cattrs 26.2.0
File Interpreter ABI Platform
cattrs-26.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 599.8 kB

Release files / cattrs-26.2.0.tar.gz

Download URL cattrs-26.2.0.tar.gz
Size 525.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3cf49f69df8326bcf17a3cb3d3d3ec4a856858fe3a7473746c9044c317d3ba55
BLAKE2b-256 checksum
How to use checksums
d6b242f4524e5479b090040b5fd8bb316dd8c65a079bb6492494ce2079dc91be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 6, 2026.

Transparency log

Release files / cattrs-26.2.0-py3-none-any.whl

Download URL cattrs-26.2.0-py3-none-any.whl
Size 74.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c680f17cd1df3a1c038ad1db8d90a9c595568cc54c4e8098e7ceb8d7b9bd826b
BLAKE2b-256 checksum
How to use checksums
f54d6b8460462012a52075ba97932197f8c141be71b8e5f7e918af08ee73424b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 6, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

26.2.0 This release

2 release files

26.1.0

2 release files

25.2.0

2 release files

25.1.0

2 release files

24.1.3

2 release files

24.1.2

2 release files

24.1.1

2 release files

24.1.0

2 release files

23.2.3

2 release files

23.2.2

2 release files

23.2.1

2 release files

23.2.0

2 release files

23.1.1

2 release files

23.1.0

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.9.2

2 release files

0.9.0

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page