microsoft/qlib · error · NotImplementedError
Please implement the `first_tasks` method.
Error message
Please implement the `first_tasks` method.
What it means
OnlineStrategy.first_tasks is an abstract hook that the base class deliberately leaves unimplemented. It is called once at the start of an online workflow (e.g. OnlineManager.first_tasks / routine initialization) to seed the initial batch of tasks, so any subclass that does not override first_tasks will crash with this NotImplementedError.
Source
Thrown at qlib/workflow/online/strategy.py:76
2. Switch models at the `test_start` (at time timestamp `T + 1` typically)
Args:
models (list): a list of models.
cur_time (pd.Dataframe): current time from OnlineManger. None for the latest.
Returns:
List[object]: a list of online models.
"""
if not trained_models:
return self.tool.online_models()
self.tool.reset_online_tag(trained_models)
return trained_models
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.View on GitHub (pinned to 79633dd950)
Solutions
- Implement first_tasks() -> List[dict] in your subclass returning the initial task configurations to train
- Use RollingStrategy (it implements first_tasks by filling the task template with the rolling generator) if rolling retraining fits your case
- Check the method name and that it takes no extra required arguments
Example fix
# before
class MyStrategy(OnlineStrategy):
def prepare_tasks(self, cur_time, **kwargs):
return []
# first_tasks missing
# after
class MyStrategy(OnlineStrategy):
def prepare_tasks(self, cur_time, **kwargs):
return []
def first_tasks(self) -> List[dict]:
return [self.task_template] Defensive patterns
Strategy: type-guard
Validate before calling
from qlib.workflow.online.strategy import OnlineStrategy
def assert_first_tasks_implemented(strategy):
if strategy.first_tasks.__func__ is OnlineStrategy.first_tasks:
raise TypeError(f'{type(strategy).__name__} must implement first_tasks') Type guard
from qlib.workflow.online.strategy import OnlineStrategy
def has_first_tasks(strategy) -> bool:
return strategy.first_tasks.__func__ is not OnlineStrategy.first_tasks Try / catch
try:
tasks = strategy.first_tasks()
except NotImplementedError:
tasks = [] # or re-raise: strategy is incomplete by contract Prevention
- Treat OnlineStrategy's NotImplementedError hooks as an implicit abstract-class contract; implement all of them
- Use RollingStrategy when rolling retraining covers the use case
- Add a unit test that instantiates your strategy and calls every documented hook
When it happens
Trigger: Instantiating a custom OnlineStrategy subclass that overrides some hooks but not first_tasks, then triggering the initial task generation; calling strategy.first_tasks() directly; misspelling the override (e.g. first_task).
Common situations: Building a custom online strategy and forgetting the bootstrap hook; adapting RollingStrategy code and deleting its first_tasks implementation; assuming the base class has a default implementation.
Related errors
- Please implement the `prepare_tasks` method.
- Please implement the `get_all_stock` method
- Please implement the `get_data` method
- Please implement the `__init__` method
- Subclass of SeriesDFilter must reimplement `getFilterSeries`
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/07fec29aa3cb6091.
Report an issue: GitHub.