Skip to content

Pipeline score asks to explicitly request sample_weight #30937

Closed

Description

@antoinebaker

Describe the bug

When using Pipeline with metadata routing enabled, an error is thrown unless we explicitly request sample_weight for the score method (see example below). But Pipeline is just a router (for both the fit and score methods) and not a consumer of sample_weight, so in principle it should not require sample_weight to be explicitly requested.

Steps/Code to Reproduce

import numpy as np
from sklearn import set_config
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import make_classification

set_config(enable_metadata_routing=True)
X, y = make_classification(10, 4)
sample_weight = np.ones_like(y)
logreg = LogisticRegression()
pipe = Pipeline([("logistic", logreg)])
search = GridSearchCV(pipe, {"logistic__C": [0.1, 1]}, n_jobs=1, cv=3)
logreg.set_fit_request(sample_weight=True)
logreg.set_score_request(sample_weight=True)
search.fit(X, y, sample_weight=sample_weight)

Expected Results

No error is thrown, and sample_weight are routed to the logreg.

Actual Results

sklearn.exceptions.UnsetMetadataPassedError: [sample_weight] are passed but are not explicitly set as requested or not requested for Pipeline.score, which is used within GridSearchCV.fit. Call `Pipeline.set_score_request({metadata}=True/False)` for each metadata you want to request/ignore.

Versions

sklearn: 1.7.dev0

Activity

  1. added
    Needs Triage
    on Mar 4, 2025
  2. added
    Metadata Routing
    and removed
    Needs Triage
    on Mar 4, 2025
  3. ogrisel commented on Mar 4, 2025

    @ogrisel
    Member
  4. adrinjalali commented on Mar 4, 2025

    @adrinjalali
    Member

    I think the solution should be very similar to LogisticRegressionCV: #30859

  5. guilhermecsnpeixoto commented on Mar 6, 2025

    @guilhermecsnpeixoto
  6. pedroL0pes commented on Mar 6, 2025

    @pedroL0pes
  7. adrinjalali commented on Mar 7, 2025

    @adrinjalali
  8. pedroL0pes commented on Mar 7, 2025

    @pedroL0pes
  9. adrinjalali commented on Aug 7, 2025

    @adrinjalali
    Member

    Digging deeper here, the issue seems to be that _PassthroughScorer doesn't correctly manage to extract the score requests on a meta-estimator:

    def __init__(self, estimator):
    self._estimator = estimator
    requests = MetadataRequest(owner=self.__class__.__name__)
    try:
    requests.score = copy.deepcopy(estimator._metadata_request.score)
    except AttributeError:
    try:
    requests.score = copy.deepcopy(estimator._get_default_requests().score)
    except AttributeError:
    pass
    self._metadata_request = requests

    I'll see how one can reliably extract a part of a larger request here. I remember having a simple copy of the whole thing wouldn't solve the issue, not sure why.

  10. moved this from Todo to In Progress in Metadata routingon Aug 11, 2025
  11. PaulOberg1 commented on Aug 11, 2025

    @PaulOberg1
  12. moved this from In Progress to Done in Metadata routingon Aug 31, 2025
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