{"record":{"id":"10c89d65a1158b75","repo":"rohitg00/ai-engineering-from-scratch","slug":"context-length-must-be-1-got-context-length","errorCode":null,"errorMessage":"context_length must be >= 1, got {context_length}","messagePattern":"context_length must be >= 1, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/31-tokenized-dataset-sliding-window/code/main.py","lineNumber":165,"sourceCode":"        out.extend(symbols)\n    return out\n\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:","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/31-tokenized-dataset-sliding-window/code/main.py#L147-L183","documentation":"SlidingWindowDataset.__init__ rejects a context_length below 1 because each example needs at least one input token plus one target token; a zero or negative window size would make count_windows produce nonsense.","triggerScenarios":"Thrown at phases/19-capstone-projects/31-tokenized-dataset-sliding-window/code/main.py:165 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Set context_length to a positive integer such as 8 or 64","Validate user-supplied config before constructing the dataset","Clamp context_length to at least 1 with an explicit warning if the value comes from a sweep"],"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"}