Skip to content

Using dask distributed with a pipeline and column_transformer breaks metadata routing #34005

Closed

Description

@tomMoral

Describe the bug and give evidence about its user-facing impact

when using a pipeline with a columns transformer, using the pipeline in a distributed environment seems to raise a strange error.

Steps/Code to Reproduce

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.compose import make_column_transformer
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import StandardScaler, QuantileTransformer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score

X, y = make_classification()

pipeline = make_pipeline(
    make_column_transformer(
        (StandardScaler(), [0]),
    ),
    # StandardScaler(),
    RandomForestClassifier(
        max_depth=7, n_estimators=300, random_state=42
    )
)
pipeline

cv_results = cross_val_score(pipeline, X, y, cv=5, n_jobs=1)
print("CV score:", np.mean(cv_results))

from distributed import Client
from joblib import parallel_config

client = Client()

with parallel_config("dask"):
    cv_scores = cross_val_score(pipeline, X, y, cv=5, n_jobs=-1)

print("CV score:", np.mean(cv_scores))
client.close()

Expected Results

CV score: 0.8300000000000001
CV score: 0.8300000000000001

Actual Results

CV score: 0.8300000000000001
warnings.warn(
/home/tom/Work/prog/scikit-learn/sklearn/model_selection/_validation.py:944: UserWarning: Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details:
joblib.externals.loky.process_executor._RemoteTraceback:
"""
Traceback (most recent call last):
File "/home/tom/Work/prog/joblib/joblib/_utils.py", line 109, in call
return self.func(**kwargs)
^^^^^^^^^^^^^^^^^^^
File "/home/tom/Work/prog/joblib/joblib/_dask.py", line 128, in call
results.append(func(*args, **kwargs))
^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/Work/prog/scikit-learn/sklearn/utils/parallel.py", line 184, in call
return self.function(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/Work/prog/scikit-learn/sklearn/pipeline.py", line 1486, in _transform_one
res = transformer.transform(X, **params.transform)
^^^^^^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'transform'
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/tom/Work/prog/scikit-learn/sklearn/metrics/_scorer.py", line 170, in call
score = scorer(estimator, *args, **routed_params.get(name).score)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/Work/prog/scikit-learn/sklearn/metrics/_scorer.py", line 485, in call
return estimator.score(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/Work/prog/scikit-learn/sklearn/pipeline.py", line 1138, in score
Xt = transform.transform(Xt)
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/Work/prog/scikit-learn/sklearn/utils/_set_output.py", line 334, in wrapped
data_to_wrap = f(self, X, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/Work/prog/scikit-learn/sklearn/compose/_column_transformer.py", line 1079, in transform
Xs = self._call_func_on_transformers(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/Work/prog/scikit-learn/sklearn/compose/_column_transformer.py", line 889, in _call_func_on_transformers
return Parallel(n_jobs=self.n_jobs)(jobs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/Work/prog/scikit-learn/sklearn/utils/parallel.py", line 91, in call
return super().call(iterable_with_config_and_warning_filters)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/Work/prog/joblib/joblib/parallel.py", line 2094, in call
return output if self.return_generator else list(output)
^^^^^^^^^^^^
File "/home/tom/Work/prog/joblib/joblib/parallel.py", line 1704, in _get_outputs
yield from self._retrieve()
File "/home/tom/Work/prog/joblib/joblib/parallel.py", line 1806, in _retrieve
self._raise_error_fast()
File "/home/tom/Work/prog/joblib/joblib/parallel.py", line 1881, in _raise_error_fast
error_job.get_result(self.timeout)
File "/home/tom/Work/prog/joblib/joblib/parallel.py", line 780, in get_result
return self._return_or_raise()
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tom/Work/prog/joblib/joblib/parallel.py", line 795, in _return_or_raise
raise self._result
AttributeError: 'dict' object has no attribute 'transform'
[...] x 5
CV score: nan

Versions

1.8.0/1.9.dev0

Interest in fixing the bug

If times allow it.

Activity

  1. added
    Needs Triage
    on May 12, 2026
  2. tomMoral commented on May 12, 2026

    @tomMoral
    ContributorAuthor

    Note that changing backend to loky fixes the behavior.
    Also, not using the column transformer (just the standard scaler) also remove the error.
    this probably comes from a bad deserialization of the params as a dict instead of a Bunch but it is unclear why it only happens for dask.

  3. added
    Metadata Routing
    on May 20, 2026
  4. removed
    Needs Triage
    on May 20, 2026
  5. adrinjalali commented on May 20, 2026

    @adrinjalali
    Member

    This is the root of the cause, which is not idea, since it's ignoring the child classes of dict:

      >>> from distributed.protocol import dumps, loads
      >>> from sklearn.utils import Bunch
      >>> type(loads(dumps(Bunch(transform={'a':1}))))
      <class 'dict'>

    and dask backend uses this. We can fix it in sklearn by not using the bunch syntax, but I think the issue needs to be fixed on the dask backend side. WDYT?

  6. StefanieSenger commented on Jun 8, 2026

    @StefanieSenger
    Member

    I am not sure if I can reproduce. Running the reproducer on main returns:

    CV score: 0.7299999999999999
    CV score: 0.89

    (Note these values are different.)

    Though I had do use

    client = Client(n_workers=2, threads_per_worker=1, memory_limit="2GB")
    and n_jobs=2

    instead of

    client = Client()
    and n_jobs=-1

    Otherwise my computer breaks down. 😆

  7. StefanieSenger commented on Jun 8, 2026

    @StefanieSenger
    Member

    Okay, the error is not stable. I ran it a few more times and could finally reproduce. 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugMetadata Routing

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions