Skip to content

Fix OverlapRefinery crash when a float context_size rounds to zero tokens#632

Open
santhreal wants to merge 1 commit into
feyninc:mainfrom
santhreal:fix/overlap-zero-context-size-crash
Open

Fix OverlapRefinery crash when a float context_size rounds to zero tokens#632
santhreal wants to merge 1 commit into
feyninc:mainfrom
santhreal:fix/overlap-zero-context-size-crash

Conversation

@santhreal

@santhreal santhreal commented Jul 16, 2026

Copy link
Copy Markdown

With a float context_size (the default is 0.25), the per-chunk overlap is int(context_size * prev_chunk.token_count). For a chunk of three or fewer tokens this rounds to 0, and:

  • mode="recursive" then splits with a zero step (range(..., 0)) and raises ValueError: range() arg 3 must not be zero;
  • mode="token", method="prefix" uses tokens[-0:], which is the whole chunk, so the entire previous chunk is copied in as overlap.

Zero tokens means no overlap, so the two overlap-context routers now return an empty context in that case. Reachable from OverlapRefinery.refine() with default settings whenever a chunk is small (a small chunk_size, or a short trailing chunk). Added three regression tests; the full overlap suite passes (52).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed overlap processing when the configured context size rounds down to zero.
    • Prevented crashes and unintended text duplication during recursive and token-based overlap handling.
    • Chunks now retain their original text with an empty context when no overlap can be applied.

…kens

With a float context_size (the default is 0.25), the per-chunk overlap size is
int(context_size * prev_chunk.token_count). For a chunk of three or fewer tokens
this rounds to 0. The recursive mode then split with a zero step
(range(..., 0)) and raised 'ValueError: range() arg 3 must not be zero', and the
token prefix mode treated tokens[-0:] as the whole chunk, copying the entire
previous chunk in as overlap.

Zero tokens means no overlap: return an empty context in that case. Reachable
from the public OverlapRefinery.refine() with default settings whenever a chunk
is small (small chunk_size, or a short trailing chunk).
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: fa4dbe89-edae-4ce1-92f8-d4ac67109d4e

📥 Commits

Reviewing files that changed from the base of the PR and between 0a6baea and 638b797.

📒 Files selected for processing (2)
  • src/chonkie/refinery/overlap.py
  • tests/refinery/test_overlap_refinery.py

📝 Walkthrough

Walkthrough

The overlap refinery now returns empty context when a computed overlap size is zero or negative. Regression tests cover recursive suffix, recursive prefix, and token-based prefix behavior with small fractional context sizes.

Changes

Overlap refinery zero-context handling

Layer / File(s) Summary
Guard zero-sized overlap contexts and add regressions
src/chonkie/refinery/overlap.py, tests/refinery/test_overlap_refinery.py
Prefix and suffix overlap helpers return empty context for non-positive effective sizes. Tests verify chunk text remains unchanged and no previous text is duplicated.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: chonk-lain

Poem

A rabbit found overlap-sized crumbs,
That rounded down to none.
It cleared the context, kept text whole,
And stopped duplication’s run.
“Hop!” said Bun, “the fix is done!”

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: preventing OverlapRefinery crashes when float context_size rounds to zero tokens.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai
coderabbitai Bot requested a review from chonk-lain July 16, 2026 07:15

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses potential crashes and unintended chunk duplication in the overlap refinery by returning an empty string early when the effective context size is less than or equal to zero. This scenario typically occurs when a float context size rounds down to zero for small chunks. Unit tests have been added to cover these edge cases for both recursive and token modes. There are no review comments, so I have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

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.

1 participant