Skip to content

API Developer API for customising metadata requests in consumers - #34314

Open
StefanieSenger wants to merge 52 commits into
scikit-learn:mainfrom
StefanieSenger:developer_API_get_metadata_request
Open

StefanieSenger wants to merge 52 commits into
scikit-learn:mainfrom
StefanieSenger:developer_API_get_metadata_request

Conversation

@StefanieSenger

@StefanieSenger StefanieSenger commented Jun 16, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

What does this implement/fix? Explain your changes.

This adds a developer API using a new method __sklearn_get_metadata_request__ for metadata consumers that customise their requests (such as _BaseScorer does internally).

This change is backwards compatible, as one can keep using the original _get_metadata_request.

AI usage disclosure

I used AI assistance for:

  • Documentation (including examples)
  • Research and understanding

CC @adrinjalali, is that how you imagined it?

@github-actions github-actions Bot added module:metrics module:utils CI:Linter failure The linter CI is failing on this PR labels Jun 16, 2026
Comment thread sklearn/tests/test_metadata_routing.py Outdated
"score": {"metadata2": False},
}

class CustomMethodConsumer(_MetadataRequester):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CustomMethodConsumer and CustomFunctionConsumer are basically the same test. We can remove one. I'd tend to remove the second one since it already gets used in plot_metadata_routing.py.

Comment thread sklearn/utils/_metadata_requests.py Outdated
request : MetadataRequest
A :class:`~sklearn.utils.metadata_routing.MetadataRequest` instance.
"""
return self.__sklearn_get_metadata_request__()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't expect this to be a trivial one-line change but everything else would be an overkill if we keep inheritance from _MetadataRequester.

@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Jun 16, 2026
# %%
# The imports below are only required for the examples in this section.

from sklearn.utils._metadata_requests import MethodMetadataRequest

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we expose MethodMetadataRequest in the public API? It looks a bit like we forgot to do that in the past, or is there a reason we didn't?

Comment on lines +608 to +609
from sklearn.utils._metadata_requests import MethodMetadataRequest
from sklearn.utils.metadata_routing import MetadataRequest, _MetadataRequester

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, we still rely on inheriting from (and importing) _MetadataRequester, but that's part of the way people should implement custom requests, right?

(The real art would be to prepare this so that CustomConsumers that set requests on custom methods. But seems not at all trivial and we can do that later, if people want it, maybe.)

@StefanieSenger StefanieSenger added Metadata Routing all issues related to metadata routing, slep006, sample props Developer API Third party developer API related labels Jun 16, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Labs Jun 16, 2026
@StefanieSenger StefanieSenger moved this from Todo to In progress in Labs Jun 16, 2026
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Jun 23, 2026
@StefanieSenger

StefanieSenger commented Jun 23, 2026 •

Copy link
Copy Markdown
Member Author

@adrinjalali, I tried your suggestion in b0ee0ac, but it's not giving expected results.

__sklearn_get_metadata_request__() should do exactly the same as _get_metadata_request() for it to adapt to each other.

But if we test if they do exactly the same, test_custom_consumer_returns_non_default_request fails even for DefaultConsumer. This is because without

            if hasattr(self, "_metadata_request"):
                requests = self._metadata_request.__sklearn_clone__()

in __sklearn_get_metadata_request__, it doesn't reflect the user-set requests.

The problem is that _get_metadata_request() is doing two very different kind of tasks. I'd be happy to split it into two functions internally. Then it will be easier to define a clean developer API for it.

I'd like to merge this PR (with the solution I had suggested before) and #31695 first and then take care of re-factoring our internal _get_metadata_request(). There are other things that I'd like to refactor and design differently too, each in a separate PR.

@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Jun 23, 2026
Comment thread sklearn/tests/test_metadata_routing.py Outdated
Comment on lines +264 to +267
assert consumer.__sklearn_get_metadata_request__()._serialize() == {
"fit": {"sample_weight": True},
"score": {"metadata": False},
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test is wrong. We don't want __sklearn_get_metadata_request__ to reflect user-set requests. It only reflects the "default" / "starting point" requests. For this test, we should simply do get_routing_for_object and see what it is.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is not wrong. It is a design question how we want to define the API.

If it stay as is __sklearn_get_metadata_request__() (qua name) should do exactly the same as _get_metadata_request(), so in this sense the test would test the right thing.

But we can split __sklearn_get_metadata_request__() off into doing only the default part (and rename it so it reflects that. Then though it makes sense to split `_get_metadata_request() into two as well (in a future PR), see issue #34372).

I will do the split-off of __sklearn_get_metadata_request__() here and change the internal function in another PR later.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why we'd need to split _get_metadata_request into two.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it gets and sets and the same time.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That function isn't setting anything, it's only returning the already set / cached values

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If users use set_fit_request, this function returns the user's requests on the instance over the class-level requests. Maybe that's not strictly setting, but it's combining too much stuff, which is hard to understand, think of and maintain. Of cause it works as it is, but the goal is a refactor to simplify metadata routing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happy to see where you'd put this logic. It has to be somewhere, and in my head this particular one can't get simpler than what it is after this PR

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this PR, I agree that the _get_metadata_request logic is fine as is, and here we split __sklearn_get_metadata_request__ to only get class-level requests (rename accordingly).

Yes, combining the class-level requests and user-set requests needs to live somewhere, but maybe it can be fully done on set_{method}_request, so getting is simply getting. We can keep this for a follow-up PR and figure out the details then.

@StefanieSenger StefanieSenger changed the title API Developer API for custom _get_metadata_request in consumers API Developer API for customising class-level metadata requests in consumers Jun 24, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Aug 27, 2026
@StefanieSenger StefanieSenger moved this from PR waiting for reviews to In progress in Labs Aug 31, 2026
@ogrisel ogrisel moved this from In progress to PR waiting for reviews in Labs Sep 7, 2026
@StefanieSenger

StefanieSenger commented Sep 8, 2026 •

Copy link
Copy Markdown
Member Author

Thank you @antoinebaker for your recent review. I have gone through your suggestions and also self-reviewed this PR thereafter.

This PR is ready for a new round of reviews now.

Also CC @adrinjalali, if you can find some time.

@antoinebaker antoinebaker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @StefanieSenger, thanks again for the PR! Apart from one nitpick, LGMT!

Comment thread sklearn/tests/test_metadata_routing.py Outdated
Comment on lines +273 to +274
def score(self, y_true, y_pred, metadata1):
return np.sum(y_true / y_pred) / len(y_true) # pragma: no cover

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would again use score(self, X, y, param1, param2, metadata1)

Comment on lines +288 to +291
ignore_params={
"y_true",
"y_pred",
},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and ignore_params = {"param1","param2"}

def fit(self, X, y, metadata1, metadata2):
return self

def score(self, y_true, y_pred, metadata1):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in the test, why not score(self, X, y, param1, param2, metadata1) and ignore_params={"param1","param2"} ? But if you prefer this signature, I'm fine with it :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have just renamed the params as you had suggested (also in the test). I had wanted to show some customisation, but I agree it is cleaner like this.

Comment on lines +622 to +626
# Note that a declaration like `__metadata_request__score = {"y_true": True}` would
# override the requests derived by signature sniffing, even when `y_true` is in
# `ignore_params`. __metadata_request__* class attributes always override other
# class-level requests, including those modified in
# `__sklearn_build_declared_metadata_request__`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this is too much detail for a user/developer guide, I would remove it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed it.

@StefanieSenger StefanieSenger left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @antoinebaker!

I applied and did your last few suggestions.

I feel this PR now is in a very good shape. Do you want to have another look, @adrinjalali?

Comment on lines +622 to +626
# Note that a declaration like `__metadata_request__score = {"y_true": True}` would
# override the requests derived by signature sniffing, even when `y_true` is in
# `ignore_params`. __metadata_request__* class attributes always override other
# class-level requests, including those modified in
# `__sklearn_build_declared_metadata_request__`.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed it.

def fit(self, X, y, metadata1, metadata2):
return self

def score(self, y_true, y_pred, metadata1):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have just renamed the params as you had suggested (also in the test). I had wanted to show some customisation, but I agree it is cleaner like this.

@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Sep 28, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Sep 28, 2026
@StefanieSenger StefanieSenger removed the status in Labs Nov 9, 2026
@StefanieSenger StefanieSenger moved this to PR waiting for reviews in Labs Nov 9, 2026
@github-actions github-actions Bot added size/L CI:Linter failure The linter CI is failing on this PR labels Nov 10, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Nov 10, 2026

This branch has not been deployed

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

Labels

Developer API Third party developer API related Metadata Routing all issues related to metadata routing, slep006, sample props module:metrics module:utils size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants