Repository navigation
fix: validate document chunker input types - #60
Open
AkashKundu114 wants to merge 4 commits into
Open
AkashKundu114 wants to merge 4 commits into
AkashKundu114 wants to merge 4 commits into
Conversation
Reviewer's GuideImplementationValidate chunking arguments and document text before processing, while preserving empty-input and overlap-clamping behavior; the same file also has related formatting-only cleanup in its indexing functions.
TestsCover empty inputs, rejected text and parameter types, and the existing overlap-clamping boundaries.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="backend/app/core/document_indexer.py" line_range="9" />
<code_context>
def chunk_text(text: str, chunk_size: int = 1000, overlap: int = 200) -> list[str]:
- if not text:
+ if text is None or text == "":
return []
+ if not isinstance(text, str):
</code_context>
<issue_to_address>
**Invalid settings are accepted**
When text is empty or None and either chunking setting has an unsupported type, `chunk_text` returns before checking `chunk_size` and `overlap`, so callers receive an empty result instead of the promised `TypeError`.
Check `chunk_size` and `overlap` before returning for empty or `None` text.
</issue_to_address>
### Comment 2
<location path="backend/app/core/document_indexer.py" line_range="9" />
<code_context>
def chunk_text(text: str, chunk_size: int = 1000, overlap: int = 200) -> list[str]:
- if not text:
+ if text is None or text == "":
return []
+ if not isinstance(text, str):
</code_context>
<issue_to_address>
**Array input raises ValueError**
When a multi-element NumPy array is passed as `text`, `text == ""` produces an array, so the `or` condition raises `ValueError` before the string-type guard and callers receive the wrong exception.
Handle `None` and validate that `text` is a string before comparing it with `""`.
</issue_to_address>
### Comment 3
<location path="tests/core/test_document_indexer.py" line_range="29-32" />
<code_context>
+ chunk_text("some content", overlap=overlap)
+
+
+def test_chunk_text_keeps_existing_overlap_clamping():
+ chunks = chunk_text("abcdefghijklmnopqrstuvwxyz", chunk_size=10, overlap=20)
+ assert chunks
+ assert all(len(chunk) <= 10 for chunk in chunks)
</code_context>
<issue_to_address>
**Overlap regression goes unnoticed**
When the implementation uses zero overlap instead of clamping the oversized overlap to `chunk_size - 1`, `test_chunk_text_keeps_existing_overlap_clamping` at lines 28–29 only checks that the result is nonempty and chunks are at most 10 characters long; with overlap clamping replaced by zero overlap, those assertions still pass and the regression goes unnoticed.
Assert the expected chunk boundaries or that adjacent chunks overlap by the clamped amount.
</issue_to_address>Sourcery assessment
Approval pending. 3 findings to address first.
Blocking findings: backend/app/core/document_indexer.py:9, backend/app/core/document_indexer.py:9, tests/core/test_document_indexer.py:32
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Noneand empty strings.Validation
tests/core/test_document_indexer.py.Summary by Sourcery
Validate document chunker inputs while preserving existing empty-input and overlap behavior.
Bug Fixes:
Tests: