{"record":{"id":"07fec29aa3cb6091","repo":"microsoft/qlib","slug":"please-implement-the-first-tasks-method","errorCode":null,"errorMessage":"Please implement the `first_tasks` method.","messagePattern":"Please implement the `first_tasks` method\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"qlib/workflow/online/strategy.py","lineNumber":76,"sourceCode":"            2. Switch models at the `test_start` (at time timestamp `T + 1` typically)\n\n        Args:\n            models (list): a list of models.\n            cur_time (pd.Dataframe): current time from OnlineManger. None for the latest.\n\n        Returns:\n            List[object]: a list of online models.\n        \"\"\"\n        if not trained_models:\n            return self.tool.online_models()\n        self.tool.reset_online_tag(trained_models)\n        return trained_models\n\n    def first_tasks(self) -> List[dict]:\n        \"\"\"\n        Generate a series of tasks firstly and return them.\n        \"\"\"\n        raise NotImplementedError(f\"Please implement the `first_tasks` method.\")\n\n    def get_collector(self) -> Collector:\n        \"\"\"\n        Get the instance of `Collector <../advanced/task_management.html#Task Collecting>`_ to collect different results of this strategy.\n\n        For example:\n            1) collect predictions in Recorder\n            2) collect signals in a txt file\n\n        Returns:\n            Collector\n        \"\"\"\n        raise NotImplementedError(f\"Please implement the `get_collector` method.\")\n\n\nclass RollingStrategy(OnlineStrategy):\n    \"\"\"\n    This example strategy always uses the latest rolling model sas online models.","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/microsoft/qlib/blob/79633dd9506ea689e5400dea0197717b5b3d74b7/qlib/workflow/online/strategy.py#L58-L94","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"# before\nclass MyStrategy(OnlineStrategy):\n    def prepare_tasks(self, cur_time, **kwargs):\n        return []\n    # first_tasks missing\n\n# after\nclass MyStrategy(OnlineStrategy):\n    def prepare_tasks(self, cur_time, **kwargs):\n        return []\n    def first_tasks(self) -> List[dict]:\n        return [self.task_template]","handlingStrategy":"type-guard","validationCode":"from qlib.workflow.online.strategy import OnlineStrategy\n\ndef assert_first_tasks_implemented(strategy):\n    if strategy.first_tasks.__func__ is OnlineStrategy.first_tasks:\n        raise TypeError(f'{type(strategy).__name__} must implement first_tasks')","typeGuard":"from qlib.workflow.online.strategy import OnlineStrategy\n\ndef has_first_tasks(strategy) -> bool:\n    return strategy.first_tasks.__func__ is not OnlineStrategy.first_tasks","tryCatchPattern":"try:\n    tasks = strategy.first_tasks()\nexcept NotImplementedError:\n    tasks = []  # or re-raise: strategy is incomplete by contract","preventionTips":["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"],"tags":["qlib","online-strategy","abstract-method","not-implemented"],"backgroundTag":null,"analyzedSha":"79633dd9506ea689e5400dea0197717b5b3d74b7","analyzedAt":"2026-08-15T07:01:27.511Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}