rohitg00/ai-engineering-from-scratch · error · ValueError
overlap_words must be between zero and max_words - 1
Error message
overlap_words must be between zero and max_words - 1
What it means
Raised in chunk_document when overlap_words is negative or >= max_words. The step size max_words - overlap_words must be at least 1 to make forward progress; outside that range the loop would stall or walk backwards and never terminate over the document.
Solutions
- Use an overlap strictly smaller than max_words (default 12 with max_words 80)
- Set overlap_words to 0 to disable overlapping windows
- Validate the overlap/window pair together in pipeline configuration
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at certifications/claude/lessons/24-rag-retrieval-and-data-pipelines/code/main.py:54 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/7b90fd7412a1428c.
Report an issue: GitHub.
Appendix: source
Thrown at certifications/claude/lessons/24-rag-retrieval-and-data-pipelines/code/main.py:54
text: str
updated_at: str
position: int
@dataclass(frozen=True)
class RetrievalHit:
chunk_id: str
document_id: str
text: str
updated_at: str
score: float
def chunk_document(document: Document, max_words: int = 80, overlap_words: int = 12) -> list[Chunk]:
if max_words <= 0:
raise ValueError("max_words must be positive")
if overlap_words < 0 or overlap_words >= max_words:
raise ValueError("overlap_words must be between zero and max_words - 1")
words = document.text.split()
if not words:
return []
step = max_words - overlap_words
chunks = []
for position, start in enumerate(range(0, len(words), step)):
selected = words[start : start + max_words]
if not selected:
break
chunks.append(
Chunk(
chunk_id=f"{document.document_id}:{position}",
document_id=document.document_id,
text=" ".join(selected),
updated_at=document.updated_at,
position=position,
)
)View on GitHub (pinned to 39ea8a1c6d)