Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Bentoml BentoML Framework PicklableModel

From Leeroopedia
Revision as of 12:06, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Bentoml_BentoML_Framework_PicklableModel.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains ML Framework, Model Serialization, Pickle, Generic Models
Last Updated 2026-02-13 15:00 GMT

Overview

Provides a generic BentoML framework adapter for saving, loading, and serving any Python object that is serializable with cloudpickle.

Description

This module implements the BentoML framework adapter for arbitrary picklable Python objects. It is the most generic framework module, suitable for any model or callable that can be serialized with cloudpickle. It uses PartialKwargsModelOptions to support partial keyword arguments per method signature.

save_model() serializes any Python object using cloudpickle.dump() and stores it in the BentoML model store under the filename saved_model.pkl (defined by SAVE_NAMESPACE + PKL_EXT). The default signature is __call__ (non-batchable). The framework context records the cloudpickle version.

load_model() retrieves the model from the store and deserializes it with cloudpickle.load(), returning the original Python object.

get() retrieves the BentoML Model metadata for a given tag, validating the module name.

get_runnable() creates a PicklableRunnable class (CPU-only, single-threaded) that loads the model and maps each signature method to a runnable method. The PartialKwargsModelOptions allows specifying default keyword arguments per method that are merged with runtime arguments.

Usage

Use this module when you need to save any picklable Python object (e.g., scikit-learn models, custom classes, lambda functions) to the BentoML model store. Access via bentoml.picklable_model.

Code Reference

Source Location

Signature

def get(tag_like: str | Tag) -> Model: ...

def load_model(bento_model: str | Tag | Model) -> ModelType: ...

def save_model(
    name: Tag | str,
    model: ModelType,
    *,
    signatures: dict[str, ModelSignature] | None = None,
    labels: t.Dict[str, str] | None = None,
    custom_objects: t.Dict[str, t.Any] | None = None,
    external_modules: t.List[ModuleType] | None = None,
    metadata: t.Dict[str, t.Any] | None = None,
) -> bentoml.Model: ...

def get_runnable(bento_model: Model) -> type[PicklableRunnable]: ...

Import

import bentoml

# Via the public API
bento_model = bentoml.picklable_model.save_model("my_model", model_obj)
loaded = bentoml.picklable_model.load_model("my_model:latest")

# Direct import
from bentoml._internal.frameworks.picklable_model import save_model, load_model, get, get_runnable

I/O Contract

Inputs

Name Type Required Description
name Tag or str Yes (save_model) Name or tag for the model in the store
model Any (picklable) Yes (save_model) Any Python object serializable with cloudpickle
tag_like str or Tag Yes (get) Tag to retrieve from the model store
bento_model str, Tag, or Model Yes (load_model) Model identifier or object to load
signatures dict[str, ModelSignature] or None No Method signatures; defaults to {"__call__": ModelSignature(batchable=False)}
labels dict[str, str] or None No User-defined labels for model management
custom_objects dict[str, Any] or None No Additional objects to save with the model
external_modules List[ModuleType] or None No Additional Python modules to bundle
metadata dict[str, Any] or None No Custom metadata for the model

Outputs

Name Type Description
bentoml.Model (save_model) bentoml.Model The saved model reference in the BentoML store
ModelType (load_model) Any The deserialized Python object
Model (get) Model BentoML Model metadata object
PicklableRunnable (get_runnable) type[Runnable] A CPU-only, single-threaded Runnable class with partial kwargs support

Usage Examples

import bentoml
from sklearn.ensemble import RandomForestClassifier

# Train a scikit-learn model
clf = RandomForestClassifier()
clf.fit(X_train, y_train)

# Save using the picklable_model framework
bento_model = bentoml.picklable_model.save_model(
    "rf_classifier",
    clf,
    signatures={"predict": bentoml.models.ModelSignature(batchable=False)},
    labels={"team": "ml"},
    metadata={"accuracy": 0.95},
)
print(f"Saved: {bento_model.tag}")

# Load the model
loaded_clf = bentoml.picklable_model.load_model("rf_classifier:latest")
predictions = loaded_clf.predict(X_test)

# Save a custom callable
class MyPredictor:
    def __call__(self, x):
        return x * 2

bento_model = bentoml.picklable_model.save_model("custom_predictor", MyPredictor())

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment