Skip to content

fix(synthesizer): avoid UnboundLocalError when a document yields 0 chunks - #2849

Merged
penguine-ip merged 1 commit into
confident-ai:mainfrom
hassaanch23:fix/synthesizer-validate-chunk-size-unboundlocal
Jul 10, 2026
Merged

penguine-ip merged 1 commit into
confident-ai:mainfrom
hassaanch23:fix/synthesizer-validate-chunk-size-unboundlocal

Conversation

@hassaanch23

@hassaanch23 hassaanch23 commented Jul 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

ContextGenerator.validate_chunk_size is meant to raise an informative ValueError when 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 confusing UnboundLocalError, hiding the real cause from the user.

Reproduction

from deepeval.synthesizer.chunking.context_generator import ContextGenerator

class FakeCollection:          # stand-in Chroma collection
    def count(self): return 0  # document produced 0 chunks

class StubEmbedder:
    def embed_text(self, x): return [0.0]
    async def a_embed_text(self, x): return [0.0]
    def embed_texts(self, xs): return [[0.0] for _ in xs]
    async def a_embed_texts(self, xs): return [[0.0] for _ in xs]

gen = ContextGenerator(document_paths=["doc.txt"], embedder=StubEmbedder(), model=<any model>)
gen.validate_chunk_size(min_contexts_per_source_file=1, collection=FakeCollection())

On main this raises:

UnboundLocalError: cannot access local variable 'adjust_chunk_size' where it is not associated with a value

instead of the intended ValueError describing the 0-chunk problem.

Root cause

In validate_chunk_size, adjust_chunk_size and adjust_overlap are only assigned inside the num_chunks > 0 and min_contexts_per_source_file > 1 and self.chunk_overlap > 0 branches, but the final combined-suggestion check evaluates them unconditionally:

if adjust_chunk_size or adjust_overlap:   # both unbound when num_chunks == 0

When a document yields 0 chunks, none of those branches run, so the names are never bound and Python raises UnboundLocalError.

Fix

  • Initialize adjust_chunk_size = False and adjust_overlap = False before the suggestion branches, so the combined check is always safe.
  • Only emit the "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:

Impossible to generate 1 contexts from a document with 0 chunks.

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 so ContextGenerator constructs offline):

  • test_validate_chunk_size_zero_chunks_raises_value_error — 0 chunks, min_contexts=1
  • test_validate_chunk_size_zero_chunks_with_overlap_raises_value_error — 0 chunks, min_contexts>1, non-zero overlap
  • test_validate_chunk_size_insufficient_chunks_still_lists_options — normal shortfall path keeps the full suggestion list
  • test_validate_chunk_size_passes_when_enough_chunks — no error when there are enough chunks

The two zero-chunk tests fail on main with UnboundLocalError and pass with this change.

Notes

…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]>
@vercel

vercel Bot commented Jul 5, 2026

Copy link
Copy Markdown

@hassaanch23 is attempting to deploy a commit to the Confident AI Team on Vercel.

A member of the Team first needs to authorize it.

@penguine-ip

Copy link
Copy Markdown
Contributor

@hassaanch23 thank you!

@penguine-ip
penguine-ip merged commit fb82cc6 into confident-ai:main Jul 10, 2026
11 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants