skip to content

After upgrading to MLflow 3.14 or later, mlflow.sklearn.log_model fails on a pipeline with a custom transformer class; why, and what are the options for logging it?

level: seniorimportance: nice to knowfreq 5%

answer

  1. a default changed in 3.14
  2. dump, then reload to verify
  3. untrusted types need an allow-list
  4. custom logic out, as code
  5. cloudpickle still allowed, with a warning

basics

~20 s

MLflow 3.14 made skops the default serialization_format for mlflow.sklearn, and skops refuses to load types it was not told to trust. List the class in skops_trusted_types, move the custom step into a models-from-code wrapper, or opt back into cloudpickle.

solid answer

~40 s

MLflow 3.14.0 changed the default `serialization_format` of `mlflow.sklearn.log_model` from `cloudpickle` to `skops`. On save, MLflow dumps the pipeline with skops and immediately reloads it. If the pipeline references a type skops does not trust — such as a custom `KeywordFlagger` transformer — the call raises "The saved sklearn model references untrusted types". Objects skops cannot serialize at all, such as classes not defined at module top level, fail with a message suggesting cloudpickle. There are three ways out. Pass `skops_trusted_types=["router_rules.KeywordFlagger"]`, which MLflow records for loading; the class must still be importable there. Keep the pipeline standard and move the custom step into a `PythonModel` logged as code with `set_model`. Or pass `serialization_format="cloudpickle"`, which logs a warning about arbitrary code on load and fails wherever `MLFLOW_ALLOW_PICKLE_DESERIALIZATION=false`.

go deeper

for a junior

Recall that MLflow 3.14 made skops the scikit-learn default and that a custom class in a pipeline needs extra handling under it.

for a middle

Explain the save-then-reload check, the difference between the untrusted-types error and the cannot-serialize error, and what skops_trusted_types records.

for a senior

Choose between trusting the type, restructuring into a models-from-code wrapper and opting out, and know which existing models a pickle-free policy would break.

for a principal

Set the platform's serialization policy: when to flip MLFLOW_ALLOW_PICKLE_DESERIALIZATION to false, how to migrate the existing catalogue first, and how to pin releases so defaults change deliberately.

## What changed Before MLflow 3.14.0, `mlflow.sklearn.log_model` serialized models with **cloudpickle** unless told otherwise. The 3.14.0 release notes list, under Breaking Changes, three new defaults: - `mlflow.sklearn`: `serialization_format` default changed from `cloudpickle` to `skops`; - `mlflow.lightgbm`: default changed to `skops`; - `mlflow.pytorch`: default changed to `"pt2"`. The documentation page on pickle-free models still calls skops "an upcoming" default. The changelog and the `mlflow.sklearn` source in 3.16.1, where `save_model` and `log_model` both declare `serialization_format=SERIALIZATION_FORMAT_SKOPS`, are authoritative. The model file is now `model.skops` and the `sklearn` flavor in `MLmodel` records `serialization_format: skops`. ## Why the call fails **skops** stores scikit-learn objects without Python's pickle, and on load it only reconstructs types it trusts. MLflow checks this at log time rather than leaving the surprise for the platform team: 1. `skops.io.dump` writes the pipeline to `model.skops`. 2. MLflow immediately calls `skops.io.load(path, trusted=skops_trusted_types)`. 3. If skops finds an untrusted type, MLflow raises: "The saved sklearn model references untrusted types. If you are sure loading these types is safe, set the 'skops_trusted_types' parameter…". 4. If skops cannot serialize the object at all, MLflow raises a different error, explaining that skops does not support custom functions or classes not defined at the top level and suggesting cloudpickle. A support-ticket router whose pipeline includes a hand-written `KeywordFlagger` transformer worked on 3.13 and fails on 3.14 with no change to its own code. ## The options | Option | What you change | What you get | |---|---|---| | Trust the type | `skops_trusted_types=["router_rules.KeywordFlagger"]` | still skops; the list is recorded in `MLmodel` and used at load; the class must be importable there, via a package or `code_paths` | | Restructure | keep the pipeline to standard scikit-learn steps; put the keyword logic in a `PythonModel` logged from a script ending in `mlflow.models.set_model(...)` | skops for the pipeline, plain code for the wrapper, no pickle anywhere | | Opt out | `serialization_format="cloudpickle"` (or `"pickle"`) | the old behaviour, plus a warning that these formats can execute arbitrary code during deserialization | Restructuring is usually the cleanest. The custom code becomes a readable script stored in the model, the pipeline stays in a format that cannot run code on load, and the wrapper is not cloudpickled either, as it would be if a `PythonModel` instance were passed. ## Models logged before the upgrade Loading reads the format recorded in each model's own `MLmodel`, so a model logged on 3.13 with cloudpickle still loads on 3.16. One exception: loading pickle and cloudpickle models is governed by **`MLFLOW_ALLOW_PICKLE_DESERIALIZATION`**, which defaults to `true` for backward compatibility. A platform that sets it to `false` gets an error for every pickled scikit-learn model and every cloudpickled `PythonModel`, with a message pointing at skops or models-from-code. That variable is the lever for making pickle-free loading a policy rather than a preference. ## Choosing between the options - **Trust the type** when the custom transformer is genuinely part of the fitted pipeline (it learns state in `fit`) and already lives in an importable, versioned module. - **Restructure** when the custom step is stateless logic such as keyword rules: it does not need to be inside the pipeline at all, and the wrapper script documents it. - **Opt out** only as a stopgap with a dated follow-up. It reintroduces the risk the new default removed, and it breaks on any platform that has disabled pickle loading. Whichever you pick, record the decision with the model so the next upgrade does not rediscover it. ## Practical checks - Pin the MLflow version in the training image so a default does not change underneath a scheduled job. - Grep the training code for custom transformer classes before upgrading; each one needs a decision. - Log once on the new release in CI with `input_example`, so the reload check and signature inference both run before a scheduled job meets the change. - Remember the same release changed PyTorch's default to `pt2`, with requirements of its own; a mixed-flavor platform has more than one migration. Why pickle can execute code on load is a security subject in its own right. Here it is enough that skops and models-from-code are MLflow's way of avoiding it.

  • Does skops_trusted_types make the custom class available where the model is loaded?
    No. It only tells skops the type is safe to reconstruct; skops still has to import the class. Ship its module as an installed package listed in the model's requirements, or pass `code_paths=["router_rules.py"]` so MLflow copies it into the model and puts it on `sys.path` before loading.
  • A platform team sets MLFLOW_ALLOW_PICKLE_DESERIALIZATION=false. Which of its existing MLflow models stop loading?
    Every model whose `MLmodel` records `pickle` or `cloudpickle` for scikit-learn — every scikit-learn model logged with default settings before 3.14 — and every `PythonModel` passed to `log_model` as an object, which is cloudpickled. Models logged with skops and wrappers logged from code keep loading.

  • MLflow 3.16 still serializes scikit-learn models with cloudpickle by default.
  • Models logged with cloudpickle before 3.14 stop loading after the upgrade.
  • skops_trusted_types makes the custom class importable where the model loads.
  • MLflow refuses to load pickled models by default since 3.14.
  • Switching to serialization_format='pickle' is the safe fix for the error.

Assumes MLflow 3.16; skops became the mlflow.sklearn default serialization_format in 3.14.0, replacing cloudpickle.