Skip to main content
Restate sends data over the network for storing state, journaling actions, awakeables, etc. Therefore, Restate needs to serialize and deserialize the journal entries.

Default Serialization

By default, the SDK serializes the journal entry with the json library. If this does not work for your data type, then you need to specify a custom serializer, as shown below.

Pydantic

Pydantic is a data validation and parsing library for Python. You can use Pydantic models to define the structure of your data: handler input/output, state, etc.

Using Pydantic

Make sure to install the optional serde dependency of the Restate SDK: restate-sdk[serde]. Then do the following:

Pydantic & OpenAPI

Pydantic integrates well with OpenAPI. Restate generates the OpenAPI specifications for you. If you use Pydantic, you can use the OpenAPI-generated clients to interact with your services. You can find example clients in the UI playground (click on your service in the overview and then on playground).

msgspec

msgspec is a fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML. You can use msgspec Structs to define the structure of your data: handler input/output, state, etc.

Using msgspec

Make sure to install the optional serde dependency of the Restate SDK: restate-sdk[serde]. Then do the following:

msgspec & OpenAPI

msgspec integrates well with OpenAPI. Restate generates the OpenAPI specifications for you. If you use msgspec, you can use the OpenAPI-generated clients to interact with your services. You can find example clients in the UI playground (click on your service in the overview and then on playground).

Dataclasses

You can also use Python’s built-in dataclasses to define the structure of your data. Make sure to install the optional serde dependency of the Restate SDK restate-sdk[serde]. Then add a type hint in a similar way as you would with Pydantic.

Custom Serialization

To write a custom serializer, you implement the Serde interface. For example a custom JSON serializer could look like this:
You then use this serializer in your handlers, as follows: