Implementation:Scikit learn Scikit learn MetadataRequests
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Metadata Routing |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete utility module for metadata routing between estimators provided by scikit-learn.
Description
The _metadata_requests module implements the metadata routing framework that allows scikit-learn meta-estimators to pass metadata (such as sample_weight) to sub-estimators. It provides MetadataRequest for consumers, MetadataRouter for routers (meta-estimators), MethodMapping for caller-callee method relationships, and process_routing for resolving metadata at call time.
Usage
Use these utilities when building custom meta-estimators that need to route metadata parameters (e.g., sample_weight, groups) to their sub-estimators, or when using the set_{method}_request API to control metadata propagation.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/utils/_metadata_requests.py
Signature
class MethodMetadataRequest:
...
class MetadataRequest:
...
class MethodMapping:
...
class MetadataRouter:
...
class RequestMethod:
...
class _MetadataRequester:
...
def process_routing(_obj, _method, /, **kwargs):
...
Import
from sklearn.utils.metadata_routing import MetadataRouter, MethodMapping, process_routing
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| _obj | estimator | Yes | The router object that is processing the routing |
| _method | str | Yes | The method name (e.g., "fit", "score") on the router |
| kwargs | dict | No | The metadata key-value pairs to be routed |
Outputs
| Name | Type | Description |
|---|---|---|
| routing_info | Bunch | Dictionary-like object with routed metadata per sub-estimator and method |
Usage Examples
Basic Usage
from sklearn.linear_model import LogisticRegression
# Request that sample_weight be passed to fit
lr = LogisticRegression()
lr.set_fit_request(sample_weight=True)
# In a meta-estimator, use process_routing to route metadata
from sklearn.utils.metadata_routing import MetadataRouter, MethodMapping
router = MetadataRouter(owner="MyMetaEstimator")
router.add(estimator=lr, method_mapping=MethodMapping().add(caller="fit", callee="fit"))