Skip to content

Better schema for numpy #7

@dylanmcreynolds

Description

@dylanmcreynolds

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 = True

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions