Commit 8c43c01
fix: reject chunk_overlap >= chunk_size in knowledge sources instead of crashing/dropping content
BaseKnowledgeSource._chunk_text() (and 6 identical copies of it duplicated
across string/json/pdf/text_file/csv/excel KnowledgeSource subclasses)
compute the slicing step as chunk_size - chunk_overlap with no validation:
range(0, len(text), chunk_size - chunk_overlap)
chunk_overlap == chunk_size -> step is 0 -> ValueError("range() arg 3 must
not be zero"), a confusing error unrelated to the actual misconfiguration.
chunk_overlap > chunk_size -> step is negative -> range(0, len(text), neg)
with start(0) < stop(len(text)) silently yields nothing -> _chunk_text
returns [] -> the source's entire content is dropped with no error, no
warning, nothing indexed.
Verified both independently with a real (non-mocked) StringKnowledgeSource
instance before this fix: chunk_overlap=chunk_size raises the confusing
range() ValueError; chunk_overlap>chunk_size raises nothing and silently
produces zero chunks.
Fix: add a single `@model_validator(mode="after")` on BaseKnowledgeSource
that rejects chunk_overlap >= chunk_size at construction time with a clear
message. Since every one of the 7 duplicated _chunk_text implementations
reads self.chunk_size/self.chunk_overlap from the same inherited Pydantic
fields, and Pydantic validators are inherited by subclasses, this one
change protects all 7 call sites without needing to touch each file's
duplicated _chunk_text individually. Confirmed via direct instantiation of
StringKnowledgeSource with the bad configs both now raise ValidationError
at construction time, before any chunking is ever attempted.
Added tests/knowledge/test_base_knowledge_source_chunk_overlap.py: equal
values rejected, overlap > size rejected, a valid overlap < size accepted,
and the library's own defaults (chunk_size=4000, chunk_overlap=200) pass
the new check. Confirmed red->green (reverting the source change reproduces
"DID NOT RAISE ValueError" for both bad-config tests). Full
tests/knowledge/ suite: 58 passed, no regressions. ruff + mypy clean.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>1 parent 0e5d0ec commit 8c43c01
2 files changed
Lines changed: 41 additions & 1 deletion
File tree
- lib/crewai
- src/crewai/knowledge/source
- tests/knowledge
Lines changed: 17 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
31 | 47 | | |
32 | 48 | | |
33 | 49 | | |
| |||
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
0 commit comments