microsoft/qlib · error · NotImplementedError
Please implement the `get_collector` method.
Error message
Please implement the `get_collector` method.
What it means
OnlineStrategy.get_collector is an abstract hook: the base class raises NotImplementedError so that each concrete strategy supplies the Collector used to gather results (predictions from recorders, signals from files, etc.). The error surfaces when OnlineManager (or user code) asks the strategy for its collector and the subclass never implemented get_collector.
Source
Thrown at qlib/workflow/online/strategy.py:89
def first_tasks(self) -> List[dict]:
"""
Generate a series of tasks firstly and return them.
"""
raise NotImplementedError(f"Please implement the `first_tasks` method.")
def get_collector(self) -> Collector:
"""
Get the instance of `Collector <../advanced/task_management.html#Task Collecting>`_ to collect different results of this strategy.
For example:
1) collect predictions in Recorder
2) collect signals in a txt file
Returns:
Collector
"""
raise NotImplementedError(f"Please implement the `get_collector` method.")
class RollingStrategy(OnlineStrategy):
"""
This example strategy always uses the latest rolling model sas online models.
"""
def __init__(
self,
name_id: str,
task_template: Union[dict, List[dict]],
rolling_gen: RollingGen,
):
"""
Init RollingStrategy.
Assumption: the str of name_id, the experiment name, and the trainer's experiment name are the same.
View on GitHub (pinned to 79633dd950)
Solutions
- Implement get_collector() -> Collector in your subclass, typically building a RecorderCollector on your experiment (see RollingStrategy.get_collector for the pattern)
- If you only need collect-from-recorder behavior, reuse or subclass RollingStrategy instead of OnlineStrategy
- Ensure the returned object is a real qlib.utils.paral / collector Collector instance, not None
Example fix
# before
class MyStrategy(OnlineStrategy):
...
# get_collector missing
# after
from qlib.workflow在线 import collector as ...
# see qlib.workflow.online.strategy.RollingStrategy
class MyStrategy(OnlineStrategy):
def get_collector(self) -> Collector:
from qlib.workflow import R
from qlib.workflow.collector import RecorderCollector
return RecorderCollector(exp_name=self.name_id, process_type=...) Defensive patterns
Strategy: type-guard
Validate before calling
from qlib.workflow.online.strategy import OnlineStrategy
def assert_collector_ready(strategy):
if strategy.get_collector.__func__ is OnlineStrategy.get_collector:
raise TypeError(f'{type(strategy).__name__} must implement get_collector before collecting') Type guard
from qlib.workflow.online.strategy import OnlineStrategy
def has_collector(strategy) -> bool:
return strategy.get_collector.__func__ is not OnlineStrategy.get_collector Try / catch
try:
collector = strategy.get_collector()
except NotImplementedError as e:
raise TypeError(f'cannot collect results: {e}') from e Prevention
- Only call collect workflows on strategies advertising a collector
- Mirror RollingStrategy.get_collector when writing custom strategies
- Keep collector construction next to the strategy class so the hook is not forgotten
When it happens
Trigger: Calling strategy.get_collector() or an OnlineManager API that collects results (e.g. OnlineManager.get_collector) on a subclass lacking the override; building a custom OnlineStrategy and forgetting this hook; returning None instead of a Collector is fine only if you never call collectors.
Common situations: First-time implementation of a custom online strategy; copying a strategy example but dropping the collector part; only needing prepare/first tasks and being surprised that collect workflows require get_collector.
Related errors
- Please implement the `prepare_tasks` method.
- Please implement the `first_tasks` method.
- Please implement the `get_all_stock` method
- Please implement the `get_data` method
- Please implement the `__init__` method
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/9f5705b301900fa7.
Report an issue: GitHub.