microsoft/qlib · error · NotImplementedError

Please implement the `_generate` method

Error message

Please implement the `_generate` method

What it means

This NotImplementedError comes from the template-method pattern in qlib's record classes: generate() checks depend_cls data, then calls self._generate(*args, **kwargs) expecting the concrete subclass to return a dict of artifacts, which generate() then saves to the recorder. The base _generate raises by design, so the error means a record subclass implemented generate's contract partially — it inherited (or reused) generate but never provided _generate.

Source

Thrown at qlib/workflow/record_temp.py:245

                logger.info("The results has previously generated, Generation skipped.")
                return

        try:
            self.check()
        except FileNotFoundError:
            logger.warning("The dependent data does not exists. Generation skipped.")
            return
        artifact_dict = self._generate(*args, **kwargs)
        if isinstance(artifact_dict, dict):
            self.save(**artifact_dict)
        return artifact_dict

    def _generate(self, *args, **kwargs) -> Dict[str, object]:
        """
        Run the concrete generating task, return the dictionary of the generated results.
        The caller method will save the results to the recorder.
        """
        raise NotImplementedError(f"Please implement the `_generate` method")


class HFSignalRecord(SignalRecord):
    """
    This is the Signal Analysis Record class that generates the analysis results such as IC and IR. This class inherits the ``RecordTemp`` class.
    """

    artifact_path = "hg_sig_analysis"
    depend_cls = SignalRecord

    def __init__(self, recorder, **kwargs):
        super().__init__(recorder=recorder)

    def generate(self):
        pred = self.load("pred.pkl")
        raw_label = self.load("label.pkl")
        long_pre, short_pre = calc_long_short_prec(pred.iloc[:, 0], raw_label.iloc[:, 0], is_alpha=True)
        ic, ric = calc_ic(pred.iloc[:, 0], raw_label.iloc[:, 0])

View on GitHub (pinned to 79633dd950)

Solutions

  1. Implement _generate(self, *args, **kwargs) -> Dict[str, object] in the subclass, returning the artifacts dict that generate() will save via self.save(**artifact_dict)
  2. Alternatively override generate() entirely in your subclass if the depend-check/save scaffolding does not fit, so _generate is never reached
  3. Keep the return type a dict mapping artifact names to objects; non-dict returns skip saving but the NotImplementedError will already have fired before that matters

Example fix

# before
class MySigRecord(HFSignalRecord):
    artifact_path = 'my_sig'
    # _generate missing -> generate() hits base _generate

# after
class MySigRecord(HFSignalRecord):
    artifact_path = 'my_sig'
    def _generate(self, *args, **kwargs) -> Dict[str, object]:
        pred = self.load('pred.pkl')
        return {'my_analysis.pkl': analyze(pred)}
Defensive patterns

Strategy: type-guard

Validate before calling

from qlib.workflow.record_temp import RecordTemp

def assert_has_generate_impl(record):
    if type(record)._generate is RecordTemp._generate:
        raise TypeError(f'{type(record).__name__} must implement _generate() to reuse the shared generate()')

Type guard

from qlib.workflow.record_temp import RecordTemp

def has_generate_worker(record) -> bool:
    return type(record)._generate is not RecordTemp._generate

Try / catch

try:
    record.generate()
except NotImplementedError as e:
    if '_generate' in str(e):
        raise TypeError(f'{type(record).__name__} forgot _generate; implement it or override generate()') from e
    raise

Prevention

When it happens

Trigger: Subclassing a record that inherits the shared generate() (the pattern used by HFSignalRecord-style classes) without overriding _generate; calling generate() on such a subclass; refactoring a record class and removing _generate while keeping the inherited generate.

Common situations: Creating custom signal-analysis records modeled on HFSignalRecord and forgetting the worker method; copy-paste of a record class where only the docstring was changed; assuming generate is the only hook to override.

Related errors


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