rohitg00/ai-engineering-from-scratch · error · ValueError
overlap must be non-negative and smaller than size
Error message
overlap must be non-negative and smaller than size
What it means
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).
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
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at phases/19-capstone-projects/64-chunking-strategies-advanced/code/main.py:42 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26).
Data as JSON: /api/errors/0ec9373e6667475e.
Report an issue: GitHub.
Appendix: source
Thrown at phases/19-capstone-projects/64-chunking-strategies-advanced/code/main.py:42
doc_id: str
strategy: str
start: int
end: int
text: str
def overlaps(self, gold_start: int, gold_end: int) -> bool:
return not (self.end <= gold_start or self.start >= gold_end)
# ---------------------------------------------------------------------------
# strategy 1 -- fixed window
# ---------------------------------------------------------------------------
def fixed_window(doc_id: str, text: str, size: int = 400, overlap: int = 80) -> list[Chunk]:
if size <= 0:
raise ValueError("size must be positive")
if overlap < 0 or overlap >= size:
raise ValueError("overlap must be non-negative and smaller than size")
out: list[Chunk] = []
step = size - overlap
i = 0
n = len(text)
while i < n:
end = min(i + size, n)
out.append(Chunk(doc_id, "fixed", i, end, text[i:end]))
if end == n:
break
i += step
return out
# ---------------------------------------------------------------------------
# strategy 2 -- sentence packer
# ---------------------------------------------------------------------------
_SENTENCE_BOUNDARY = re.compile(r"(?<=[.!?])\s+(?=[A-Z0-9])")View on GitHub (pinned to 39ea8a1c6d)