Peter Kim
11/15/2023, 2:14 PMBaseModel
objects? When I run this code snippet:
from pydantic import BaseModel
from prefect.serializers import JSONSerializer
class M(BaseModel):
f1: str
f2: int
m = M(f1="model", m2=1234)
ser = JSONSerializer(dumps_kwargs={"sort_keys": True})
ser.dumps(((m,), {}))
I get this error:
python3.10/site-packages/pydantic/v1/json.py:90, in pydantic_encoder(obj)
88 return encoder(obj)
89 else: # We have exited the for loop without finding a suitable encoder
---> 90 raise TypeError(f"Object of type '{obj.__class__.__name__}' is not JSON serializable")
TypeError: Object of type 'M' is not JSON serializable
Marvin
11/15/2023, 2:14 PMBaseModel
objects as it adds support for all types supported by Pydantic. However, you might be running into issues because of the way you're trying to serialize the Pydantic model.
When you're trying to serialize a Pydantic model, you should first convert it to a dictionary using the .dict()
method, like so:
ser.dumps(((m.dict(),), {}))
This is because the JSONSerializer expects a JSON serializable object, and a Pydantic model is not directly serializable - it needs to be converted to a dictionary first.
Here's the corrected code:
from pydantic import BaseModel
from prefect.serializers import JSONSerializer
class M(BaseModel):
f1: str
f2: int
m = M(f1="model", f2=1234)
ser = JSONSerializer(dumps_kwargs={"sort_keys": True})
ser.dumps(((m.dict(),), {}))
This should serialize your Pydantic model without any issues.Peter Kim
11/16/2023, 9:59 PMBaseModel
instances serializable with JSONPeter Kim
11/16/2023, 10:00 PMPeter Kim
11/16/2023, 10:00 PMfrom prefect.serializers import JSONSerializer
from pydantic import BaseModel
class M(BaseModel):
f1: str
f2: int
ser = JSONSerializer(dumps_kwargs={"sort_keys": True})
m = M(f1="asdf", f2=1234)
ser.dumps(((m,), {})) # Causes TypeError, which then triggers the cloud pickler in the `hash_objects` function