JoblibModelSerializer#

class openstef_models.integrations.joblib.JoblibModelSerializer(**data: Any) None[source]

Bases: ModelSerializer

File-based model storage using joblib serialization.

Provides persistent storage for ForecastingModel instances on the local filesystem. Models are serialized using joblib and stored as pickle files in the specified directory.

This storage implementation is suitable for development, testing, and single-machine deployments where simple file-based persistence is sufficient.

Note

joblib.dump() and joblib.load() are based on the Python pickle serialization model, which means that arbitrary Python code can be executed when loading a serialized object with joblib.load().

joblib.load() should therefore never be used to load objects from an untrusted source or otherwise you will introduce a security vulnerability in your program.

Invariants

  • Models are stored as .pkl files in the configured storage directory

  • Model files use the pattern: {model_id}.pkl

  • Storage directory is created automatically if it doesn’t exist

  • Load operations fail with ModelNotFoundError if model file doesn’t exist

Example

Basic usage with model persistence

>>> from pathlib import Path
>>> from openstef_models.models.forecasting_model import ForecastingModel
>>> storage = LocalModelStorage(storage_dir=Path("./models"))
>>> storage.save_model("my_model", my_forecasting_model)
>>> loaded_model = storage.load_model("my_model")
Parameters:

data (Any)

extension: ClassVar[str] = 'joblib'
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

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].