ModelSerializer#

class openstef_models.mixins.model_serializer.ModelSerializer(**data: Any) None[source]

Bases: BaseConfig, ABC

Abstract base class for model serialization.

Defines the interface for converting trained models to and from binary format. Implementations handle the mechanics of serializing model state using specific libraries like joblib, pickle, or custom formats.

The serializer ensures that all stateful components of a model can be persisted and restored, enabling model reuse across sessions and deployments.

Invariants

  • Serializing and deserializing a model preserves its state

  • The extension attribute specifies the file extension for saved models

  • Deserialized models are functionally equivalent to their original state

See also

JoblibModelSerializer: Concrete implementation using joblib.

Parameters:

data (Any)

extension: ClassVar[str]
abstractmethod serialize(model: object, file: BinaryIO) None[source]

Write a model’s state to a binary file.

Converts the model’s internal state to a binary format and writes it to the provided file object. The serialization must capture all information needed to restore the model to its current state.

Parameters:
  • model (object) – The stateful model to serialize.

  • file (BinaryIO) – Binary file object opened for writing.

  • model

  • file

Return type:

None

abstractmethod deserialize(file: BinaryIO) object[source]

Read a model’s state from a binary file and restore it.

Loads the model state from the binary file and applies it to the provided model instance. The model should be functionally equivalent to the state when it was serialized.

Parameters:
  • model – The model instance to populate with the loaded state.

  • file (BinaryIO) – Binary file object opened for reading, positioned at the start of the serialized model data.

  • file

Returns:

The same model instance with its state restored from the file.

Return type:

object

model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': False, 'extra': 'ignore', 'protected_namespaces': ()}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].