{"record":{"id":"3e2a6fc405af2380","repo":"rohitg00/ai-engineering-from-scratch","slug":"ids-must-be-non-empty","errorCode":null,"errorMessage":"ids must be non-empty","messagePattern":"ids must be non-empty","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/31-tokenized-dataset-sliding-window/code/main.py","lineNumber":167,"sourceCode":"\n\nclass SlidingWindowDataset(Dataset):\n    \"\"\"PyTorch Dataset over a flat id stream.\n\n    Each example is a window of size T+1. __getitem__ returns\n    (input_ids, target_ids) where target = input shifted left by one.\n    \"\"\"\n\n    def __init__(\n        self,\n        ids: list[int],\n        context_length: int,\n        stride: int | None = None,\n    ) -> None:\n        if context_length < 1:\n            raise ValueError(f\"context_length must be >= 1, got {context_length}\")\n        if not ids:\n            raise ValueError(\"ids must be non-empty\")\n        if stride is None:\n            stride = context_length\n        if stride < 1:\n            raise ValueError(f\"stride must be >= 1, got {stride}\")\n        self.ids = torch.tensor(ids, dtype=torch.long)\n        self.context_length = context_length\n        self.stride = stride\n\n    @staticmethod\n    def count_windows(num_ids: int, context_length: int, stride: int) -> int:\n        usable = num_ids - (context_length + 1)\n        if usable < 0:\n            return 0\n        return 1 + usable // stride\n\n    def __len__(self) -> int:\n        return self.count_windows(self.ids.numel(), self.context_length, self.stride)\n","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/31-tokenized-dataset-sliding-window/code/main.py#L149-L185","documentation":"SlidingWindowDataset.__init__ guards against an empty id stream: with no tokens there are no windows of size context_length+1, so the Dataset would have length 0 and DataLoader iteration would silently yield nothing.","triggerScenarios":"Thrown at phases/19-capstone-projects/31-tokenized-dataset-sliding-window/code/main.py:167 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check that the corpus encoded to a non-empty id list before building the dataset","Skip empty documents during corpus preprocessing","Raise a clearer upstream error when a file fails to tokenize to any ids"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}