{"record":{"id":"0ec9373e6667475e","repo":"rohitg00/ai-engineering-from-scratch","slug":"overlap-must-be-non-negative-and-smaller-than-size","errorCode":null,"errorMessage":"overlap must be non-negative and smaller than size","messagePattern":"overlap must be non-negative and smaller than size","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/64-chunking-strategies-advanced/code/main.py","lineNumber":42,"sourceCode":"    doc_id: str\n    strategy: str\n    start: int\n    end: int\n    text: str\n\n    def overlaps(self, gold_start: int, gold_end: int) -> bool:\n        return not (self.end <= gold_start or self.start >= gold_end)\n\n\n# ---------------------------------------------------------------------------\n# strategy 1  --  fixed window\n# ---------------------------------------------------------------------------\n\ndef fixed_window(doc_id: str, text: str, size: int = 400, overlap: int = 80) -> list[Chunk]:\n    if size <= 0:\n        raise ValueError(\"size must be positive\")\n    if overlap < 0 or overlap >= size:\n        raise ValueError(\"overlap must be non-negative and smaller than size\")\n    out: list[Chunk] = []\n    step = size - overlap\n    i = 0\n    n = len(text)\n    while i < n:\n        end = min(i + size, n)\n        out.append(Chunk(doc_id, \"fixed\", i, end, text[i:end]))\n        if end == n:\n            break\n        i += step\n    return out\n\n\n# ---------------------------------------------------------------------------\n# strategy 2  --  sentence packer\n# ---------------------------------------------------------------------------\n\n_SENTENCE_BOUNDARY = re.compile(r\"(?<=[.!?])\\s+(?=[A-Z0-9])\")","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/64-chunking-strategies-advanced/code/main.py#L24-L60","documentation":"fixed_window advances by (size - overlap) each step; an overlap that is negative, zero-progress, or >= size would make the stride non-positive and produce duplicate or overlapping-undefined windows, so overlap is constrained to [0, size).","triggerScenarios":"Thrown at phases/19-capstone-projects/64-chunking-strategies-advanced/code/main.py:42 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Choose 0 <= overlap < size (defaults are overlap=80, size=400)","Compute overlap as a fraction of size, e.g. overlap = int(0.2 * size), to keep the invariant automatic","Reject user-supplied overlap ratios outside [0, 1) at the config layer"],"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"}