Skip to main content

How to Find the Untrusted Types in a scikit-learn Model with skops

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

skops won't load a model file that holds a type it doesn't trust by default, and some scikit-learn models hold one. skops.io.get_untrusted_types lists those types before you load anything, so you can review each one and pass only those you trust to load. This guide does it with a gradient-boosted tree model, with real output from skops 0.16.0 and scikit-learn 1.9.1.

Syntax​

Syntax
untrusted = sio.get_untrusted_types(file=path)
model = sio.load(path, trusted=reviewed_types)
PartWhat it means
sioskops.io, imported as import skops.io as sio
get_untrusted_types(file=path)Reads the saved file and returns the names of the types skops won't load by default, without building anything
trusted=reviewed_typesThe types you reviewed and decided to trust, written out as a list of names

Prerequisites​

  • skops in your project: uv add skops.

Step 1: List the Untrusted Types and Load with a Reviewed List​

The example saves a HistGradientBoostingClassifier trained on scikit-learn's iris flowers, lists the types skops doesn't trust in the file, then loads it with the one type reviewed:

The untrusted_types.py script in a macOS-style code window: it trains a HistGradientBoostingClassifier on iris, dumps it to trees.skops, prints get_untrusted_types for that file, then loads it with trusted set to a written-out list holding TreePredictor and prints three predictions.

untrusted_types.py
import skops.io as sio
from sklearn.datasets import load_iris
from sklearn.ensemble import HistGradientBoostingClassifier

X, y = load_iris(return_X_y=True, as_frame=True)
model = HistGradientBoostingClassifier(random_state=0).fit(X, y)
sio.dump(model, "trees.skops")

print("Untrusted:", sio.get_untrusted_types(file="trees.skops"))

# Reviewed: the trees a HistGradientBoosting model is made of.
TRUSTED_TYPES = ["sklearn.ensemble._hist_gradient_boosting.predictor.TreePredictor"]
loaded = sio.load("trees.skops", trusted=TRUSTED_TYPES)
print(type(loaded).__name__, loaded.predict(X.head(3)))
Terminal
uv run python untrusted_types.py
Output
Untrusted: ['sklearn.ensemble._hist_gradient_boosting.predictor.TreePredictor']
HistGradientBoostingClassifier [0 0 0]

The output of untrusted_types.py in a terminal: Untrusted lists sklearn.ensemble._hist_gradient_boosting.predictor.TreePredictor, then the loaded HistGradientBoostingClassifier predicts 0 0 0.

One type isn't trusted by default: TreePredictor, the object each of the model's trees is stored in. Listed in trusted, the file loads and predicts.

The list is written out rather than taken from get_untrusted_types directly on purpose: the point of the check is that a person looked at each type.

Step 2: See What Happens Without trusted​

load_untrusted.py loads the same file without a trusted list:

load_untrusted.py
import skops.io as sio

sio.load("trees.skops")
Terminal
uv run python load_untrusted.py
Output (excerpt)
skops.io.exceptions.UntrustedTypesFoundException: Untrusted types found in the file: ['sklearn.ensemble._hist_gradient_boosting.predictor.TreePredictor'].

- sklearn.ensemble._hist_gradient_boosting.predictor.TreePredictor: sklearn.ensemble._hist_gradient_boosting.predictor.TreePredictor (used by HistGradientBoosting* models) stores raw node indices (left, right, feature_idx) in its nodes array that scikit-learn indexes into without bounds checking. A malicious file can set these to out-of-range values: skops loads the object successfully, but calling .predict() on it can then crash the process (segfault) or read out-of-bounds memory. If you created the file yourself or otherwise fully trust its source, you can load it with trusted=["sklearn.ensemble._hist_gradient_boosting.predictor.TreePredictor"].

Only add the specific types you have reviewed and trust to the `trusted` argument; avoid passing everything reported by get_untrusted_types() just to make a file load.

The end of the error in a terminal: UntrustedTypesFoundException naming TreePredictor, skops's explanation of why that type is risky, and its advice to add only the types you have reviewed and trust.

skops explains the risk for this type: a crafted file can hold node numbers that point outside the tree, so predicting with it could crash or read memory it shouldn't. For a file you made yourself, as here, trusting it is safe.

Common Mistakes​

MistakeWhat happensDo this instead
trusted=sio.get_untrusted_types(file=path)Every type is trusted without anyone lookingWrite out the types you reviewed
Trusting a type for a file from an unknown sourceThe protection skops gives is gone for that typeLoad such files only from sources you trust

Troubleshooting​

SymptomLikely causeFix
UntrustedTypesFoundException: Untrusted types found in the file: [...]The file holds types skops doesn't trust by defaultReview them, then pass them in trusted
get_untrusted_types returns []Every type in the file is trusted by defaultload needs no trusted list

FAQ​

Do all scikit-learn models have untrusted types?​

No. The logistic regression in How to Save and Load a scikit-learn Model with skops loaded without any. Models with special internal objects, like the gradient-boosted trees here, need them listed.

Where do I find a type's risk?​

In the exception: skops 0.16 explains the known risk for types like TreePredictor right in the message.

Conclusion​

get_untrusted_types named the one type skops wouldn't load by default, TreePredictor. Without a trusted list, load stopped with UntrustedTypesFoundException and explained the risk; with the reviewed type written out, the model loaded and predicted.

Additional Resources​