{"record":{"id":"c5d932a4e3a9ff0e","repo":"microsoft/qlib","slug":"unsupported-reweighter-type-c5d932","errorCode":null,"errorMessage":"Unsupported reweighter type.","messagePattern":"Unsupported reweighter type\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"qlib/contrib/model/xgboost.py","lineNumber":54,"sourceCode":"            data_key=DataHandlerLP.DK_L,\n        )\n        x_train, y_train = df_train[\"feature\"], df_train[\"label\"]\n        x_valid, y_valid = df_valid[\"feature\"], df_valid[\"label\"]\n\n        # Lightgbm need 1D array as its label\n        if y_train.values.ndim == 2 and y_train.values.shape[1] == 1:\n            y_train_1d, y_valid_1d = np.squeeze(y_train.values), np.squeeze(y_valid.values)\n        else:\n            raise ValueError(\"XGBoost doesn't support multi-label training\")\n\n        if reweighter is None:\n            w_train = None\n            w_valid = None\n        elif isinstance(reweighter, Reweighter):\n            w_train = reweighter.reweight(df_train)\n            w_valid = reweighter.reweight(df_valid)\n        else:\n            raise ValueError(\"Unsupported reweighter type.\")\n\n        dtrain = xgb.DMatrix(x_train.values, label=y_train_1d, weight=w_train)\n        dvalid = xgb.DMatrix(x_valid.values, label=y_valid_1d, weight=w_valid)\n        self.model = xgb.train(\n            self._params,\n            dtrain=dtrain,\n            num_boost_round=num_boost_round,\n            evals=[(dtrain, \"train\"), (dvalid, \"valid\")],\n            early_stopping_rounds=early_stopping_rounds,\n            verbose_eval=verbose_eval,\n            evals_result=evals_result,\n            **kwargs,\n        )\n        evals_result[\"train\"] = list(evals_result[\"train\"].values())[0]\n        evals_result[\"valid\"] = list(evals_result[\"valid\"].values())[0]\n\n    def predict(self, dataset: DatasetH, segment: Union[Text, slice] = \"test\"):\n        if self.model is None:","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/contrib/model/xgboost.py#L36-L72","documentation":"Raised by XGBModel.fit when the reweighter argument is neither None nor an instance of qlib's Reweighter. The wrapper only knows how to produce sample weights from a Reweighter object; any other type (dict, function, array) is rejected before xgb.train is called.","triggerScenarios":"Passing reweighter=some_dict, reweighter=np.array([...]), reweighter=lambda df: ..., or a custom class that duck-types reweight() but does not subclass/instantiate qlib.data.dataset.weight.Reweighted.","commonSituations":"Users coming from sklearn's sample_weight interface passing raw arrays; attempting custom weighting logic with a plain function instead of wrapping it in Reweighter; passing a serialized config dict instead of an instantiated object.","solutions":["Wrap your weighting logic in qlib's Reweighter (from qlib.data.dataset import Reweighter), e.g. Reweighter(name='feature', weight=df['my_weight']).","Pass reweighter=None (or omit it) if you do not need sample weighting.","If you built a custom class, make it a Reweighter subclass or just instantiate Reweighter with the right name/weight so isinstance() passes."],"exampleFix":"# before\nmodel.fit(dataset, reweighter={\"weight\": w})  # ValueError: Unsupported reweighter type.\n\n# after\nfrom qlib.data.dataset import Reweighter\nrw = Reweighter(name=\"sample\", weight=my_weight_df)  # proper Reweighter\nmodel.fit(dataset, reweighter=rw)","handlingStrategy":"type-guard","validationCode":"from qlib.data.dataset import Reweighter\n\nif reweighter is not None and not isinstance(reweighter, Reweighter):\n    raise TypeError(\"reweighter must be None or qlib Reweighter\")\nmodel.fit(dataset, reweighter=reweighter)","typeGuard":"from qlib.data.dataset import Reweighter\n\ndef is_valid_reweighter(rw) -> bool:\n    return rw is None or isinstance(rw, Reweighter)","tryCatchPattern":null,"preventionTips":["Always construct weights via Reweighter, never raw arrays/dicts/functions.","Add an isinstance check in shared training utilities.","Keep reweighter configs as (name, weight) pairs and instantiate at the boundary."],"tags":["qlib","xgboost","reweighter","arguments","type-validation"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}