Skip to content

Instantly share code, notes, and snippets.

@shrekris-anyscale
Created July 21, 2023 21:44
Show Gist options
  • Save shrekris-anyscale/3d7836a6e606dcc8272c1f22a5cf4ccf to your computer and use it in GitHub Desktop.
Save shrekris-anyscale/3d7836a6e606dcc8272c1f22a5cf4ccf to your computer and use it in GitHub Desktop.
from pydantic import BaseModel
from pydantic_core._pydantic_core import SchemaSerializer
# This also works with vanilla pickle
import cloudpickle
# Storing these as globals as a workaround.
# We should store them as instance attributes.
schema_cache, config_cache = None, None
def new_init(self, schema, config):
global schema_cache, config_cache
schema_cache = schema
config_cache = config
SchemaSerializer.__init__ = new_init
def new_reduce(self):
global schema_cache, config_cache
return SchemaSerializer, (schema_cache, config_cache, )
SchemaSerializer.__reduce__ = new_reduce
class SimpleModel(BaseModel):
val: int
sm = SimpleModel(val=5)
NewSimpleModel = cloudpickle.loads(cloudpickle.dumps(SimpleModel))
new_sm = NewSimpleModel(val=5)
print(sm == new_sm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment