rohitg00/ai-engineering-from-scratch · error · ValueError
size must be positive
Error message
size must be positive
What it means
A generic argument guard at the top of the fixed-window chunker: fixed_window refuses to build chunks when the caller supplies a window size of zero or a negative number, because no positive-length slice of text could ever be produced. It fires before any slicing happens, so it always indicates a bad caller-supplied size (e.g. a config typo or an uninitialized variable), not a problem with the input text itself.
Solutions
- Use a positive window size (the default is 400 characters)
- Load window size from config with a bounds check before chunking
- For token-based chunking, validate the converted token count is positive
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at phases/19-capstone-projects/64-chunking-strategies-advanced/code/main.py:40 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/68f4461ed739431c.
Report an issue: GitHub.
Appendix: source
Thrown at phases/19-capstone-projects/64-chunking-strategies-advanced/code/main.py:40
@dataclass
class Chunk:
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
# ---------------------------------------------------------------------------View on GitHub (pinned to 39ea8a1c6d)