microsoft/qlib · error · ValueError

model is not fitted yet!

Error message

model is not fitted yet!

What it means

Thrown by LGBModel.predict when self.model is None, i.e. predict runs before a successful fit. The LightGBM booster only exists after lgb.train inside fit, so a fresh or failed-to-fit LGBModel cannot predict.

Source

Thrown at qlib/contrib/model/gbdt.py:94

        evals_result_callback = lgb.record_evaluation(evals_result)
        self.model = lgb.train(
            self.params,
            ds[0],  # training dataset
            num_boost_round=self.num_boost_round if num_boost_round is None else num_boost_round,
            valid_sets=ds,
            valid_names=names,
            callbacks=[early_stopping_callback, verbose_eval_callback, evals_result_callback],
            **kwargs,
        )
        for k in names:
            for key, val in evals_result[k].items():
                name = f"{key}.{k}"
                for epoch, m in enumerate(val):
                    R.log_metrics(**{name.replace("@", "_"): m}, step=epoch)

    def predict(self, dataset: DatasetH, segment: Union[Text, slice] = "test"):
        if self.model is None:
            raise ValueError("model is not fitted yet!")
        x_test = dataset.prepare(segment, col_set="feature", data_key=DataHandlerLP.DK_I)
        return pd.Series(self.model.predict(x_test.values), index=x_test.index)

    def finetune(self, dataset: DatasetH, num_boost_round=10, verbose_eval=20, reweighter=None):
        """
        finetune model

        Parameters
        ----------
        dataset : DatasetH
            dataset for finetuning
        num_boost_round : int
            number of round to finetune model
        verbose_eval : int
            verbose level
        """
        # Based on existing model and finetune by train more rounds
        ds_l = self._prepare_data(dataset, reweighter)

View on GitHub (pinned to 79633dd950)

Solutions

  1. Call fit(dataset) successfully before predict(dataset)
  2. Fix any prior fit failure first (most commonly errors 152/153/154 in this file)
  3. Order operations correctly: fit -> finetune -> predict

Example fix

# before
model = LGBModel()
model.predict(dataset)  # ValueError

# after
model.fit(dataset)
model.predict(dataset)
Defensive patterns

Strategy: validation

Validate before calling

assert model.model is not None, "call fit() before predict()"

Prevention

When it happens

Trigger: model.predict(dataset) on a newly constructed LGBModel; fit raised earlier (empty data, multi-label) and the pipeline continued to predict; calling finetune (which requires an existing model) in the wrong order.

Common situations: Workflow misconfiguration where the train task is skipped; error swallowing that lets the prediction task start anyway.

Related errors


AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15). Data as JSON: /api/errors/dfaca0589bac6853. Report an issue: GitHub.