Default Serialization
By default, the SDK serializes the journal entry with thejson
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 optionalserde
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).Dataclasses
You can also use Python’s built-indataclasses
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 theSerde
interface.
For example a custom JSON serializer could look like this: