Florian Giroud
11/27/2024, 8:57 AMMarvin
11/27/2024, 8:57 AMFlorian Giroud
11/27/2024, 10:26 AMutilies.pydantic
, that if the object inherit from BaseModel
, then Prefect simply calls model_dump
• So I’ve made my Snowflake class extending BaseModel, implements model_dump
• Problem solved
Do we think this is a viable solution ?Nate
11/27/2024, 4:18 PMfrom pydantic import BaseModel, ConfigDict, model_serializer
from prefect import flow, task
from prefect.cache_policies import INPUTS
class NotSerializable:
"""A class that refuses to be serialized"""
def __getstate__(self):
raise TypeError("I refuse to be serialized!")
class Wrapper(BaseModel):
"""Example of controlling what gets cached for a non-serializable object"""
model_config = ConfigDict(arbitrary_types_allowed=True)
name: str
bad_object: NotSerializable
@model_serializer
def ser_model(self) -> dict:
"""Only serialize the name, not the problematic object"""
return {"name": self.name}
@task(cache_policy=INPUTS)
def use_object(wrapper: Wrapper) -> str:
return f"Used {wrapper.name}"
@flow
def demo_flow():
wrapper = Wrapper(name="test", bad_object=NotSerializable())
return use_object(wrapper)
if __name__ == "__main__":
print(demo_flow())
Nate
11/27/2024, 4:18 PMNate
11/27/2024, 4:19 PM@model_serializer
def ser_model(self) -> dict:
"""Only serialize the name, not the problematic object"""
return {"name": self.name}
it will fail like
ValueError: Unable to create hash - objects could not be serialized.
JSON error: Unable to serialize unknown type: <class '__main__.NotSerializable'>
Pickle error: I refuse to be serialized!
This often occurs when task inputs contain objects that cannot be cached like locks, file handles, or other system resources.
To resolve this, you can:
1. Exclude these arguments by defining a custom `cache_key_fn`
2. Disable caching by passing `cache_policy=NONE`
Florian Giroud
11/27/2024, 5:28 PMNate
11/27/2024, 5:29 PM