Skip to main content

Custom serialization

In this tutorial, we demonstrate how developers can flexibily and portably define their own classes in Superduper. These may be exported with Component.export and transported to other Superduper deployments with db.apply.

To make our lives difficult, we'll include a data blob in the model, which should be serialized with the exported class:

!curl -O https://superduperdb-public-demo.s3.amazonaws.com/text.json
import json

with open('text.json') as f:
data = json.load(f)

We'll define our own Model descendant, with a custom .predict method. We are free to define any of our own parameters to this class with a simple annotation in the header, since Model is a dataclasses.dataclass:

from superduper import *


requires_packages(['openai', None, None])


class NewModel(Model):
a: int = 2
b: list

def predict(self, x):
return x * self.a

If we want b to be saved as a blob in the db.artifact_store we can simply annotate this in the artifacts=... parameter, supplying the serializer we would like to use:

m = NewModel('test-hg', a=2, b=data, artifacts={'b': pickle_serializer})

Now we can export the model:

m.export('test-hg')
!cat test-hg/component.json
!ls test-hg/blobs/

The following cell works even after restarting the kernel. That means the exported component is now completely portable!

from superduper import *

c = Component.read('test-hg')

c.predict(2)