Repository navigation
fix(synthesizer): avoid UnboundLocalError when a document yields 0 chunks - #2849
Merged
penguine-ip merged 1 commit intoJul 10, 2026
Conversation
…unks `ContextGenerator.validate_chunk_size` builds a helpful `ValueError` when a document produces fewer chunks than the requested number of contexts. The final "adjust both" suggestion is gated on `adjust_chunk_size or adjust_overlap`, but those flags are only assigned inside the `num_chunks > 0` and `min_contexts_per_source_file > 1 and chunk_overlap > 0` branches. When a document yields 0 chunks (empty / whitespace-only / unparseable file), none of those branches run, so the flags are unbound and the intended `ValueError` is replaced by a confusing `UnboundLocalError: cannot access local variable 'adjust_chunk_size'`, masking the real problem from the user. Initialize both flags to `False` up front, and only emit the "You have the following options:" header when at least one suggestion exists so the 0-chunk message no longer dangles. Adds regression tests (no API key required) that cover both 0-chunk branches, the normal shortfall path, and the pass-through case. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
@hassaanch23 is attempting to deploy a commit to the Confident AI Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
|
@hassaanch23 thank you! |
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
ContextGenerator.validate_chunk_sizeis meant to raise an informativeValueErrorwhen a source document produces fewer chunks than the requested number of contexts. Instead, when a document yields 0 chunks (e.g. an empty / whitespace-only / unparseable file), it raises a confusingUnboundLocalError, hiding the real cause from the user.Reproduction
On
mainthis raises:instead of the intended
ValueErrordescribing the 0-chunk problem.Root cause
In
validate_chunk_size,adjust_chunk_sizeandadjust_overlapare only assigned inside thenum_chunks > 0andmin_contexts_per_source_file > 1 and self.chunk_overlap > 0branches, but the final combined-suggestion check evaluates them unconditionally:When a document yields 0 chunks, none of those branches run, so the names are never bound and Python raises
UnboundLocalError.Fix
adjust_chunk_size = Falseandadjust_overlap = Falsebefore the suggestion branches, so the combined check is always safe."You have the following options:"header when there is at least one concrete suggestion, so the 0-chunk message no longer dangles with an empty options list.After the fix, a 0-chunk document produces a clean, actionable error:
and the existing multi-suggestion messages are unchanged for the normal shortfall cases.
Tests
Adds four regression tests in
tests/test_core/test_synthesizer/test_context_generator.py(no API key required — a stub LLM is injected soContextGeneratorconstructs offline):test_validate_chunk_size_zero_chunks_raises_value_error— 0 chunks,min_contexts=1test_validate_chunk_size_zero_chunks_with_overlap_raises_value_error— 0 chunks,min_contexts>1, non-zero overlaptest_validate_chunk_size_insufficient_chunks_still_lists_options— normal shortfall path keeps the full suggestion listtest_validate_chunk_size_passes_when_enough_chunks— no error when there are enough chunksThe two zero-chunk tests fail on
mainwithUnboundLocalErrorand pass with this change.Notes
min_contexts_per_documentwording that fix(synthesizer): correct parameter name in validate_chunk_size error message #2759 corrects (that is a separate line), so there is no conflict.black.