-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
While using this in a project, I found that the NumpyArrayModel wasn't serializable. I created my own model in that project, and I'd like to add this to this repo:
class SerializableNumpyArrayModel(BaseModel):
"""
Custom Pydantic model for serializing NumPy arrays.
"""
array: np.ndarray # Define array as a NumPy array field
@field_serializer("array")
def serialize_array(self, value: np.ndarray):
"""Convert NumPy array to a dictionary with bytes and dtype"""
return {"data": value.tobytes(), "dtype": str(value.dtype.name), "shape": value.shape}
@field_validator("array", mode="before")
@classmethod
def deserialize_array(cls, value, **kwargs):
"""Convert bytes back to NumPy array"""
if isinstance(value, bytes):
return np.frombuffer(value, dtype=kwargs['dtype']).reshape(kwargs['shape'])
return value
class Config:
arbitrary_types_allowed = TrueMetadata
Metadata
Assignees
Labels
No labels