Hmbown/CodeWhale · error · ValueError

overlap must be smaller than max_chars

Error message

overlap must be smaller than max_chars

What it means

chunk() requires the overlap between consecutive chunks to be strictly smaller than max_chars; overlap >= max_chars would make chunks never advance and loop forever. The function clamps negative overlap to 0, then raises this ValueError when overlap is still too large.

Solutions

  1. Lower overlap so it is strictly less than max_chars (e.g. overlap = max_chars // 10)
  2. Raise max_chars if the desired overlap is genuinely large
  3. Clamp in caller code: overlap = min(overlap, max_chars - 1)

Example fix

// before
chunk(max_chars=1000, overlap=1000)
// after
chunk(max_chars=1000, overlap=100)
Defensive patterns

Strategy: validation

Validate before calling

overlap = Math.max(0, Math.trunc(overlap));
if (overlap >= maxChars) overlap = Math.max(0, maxChars - 1);

Type guard

const validChunking = (mc, ov) => Number.isInteger(mc) && mc > 0 && Number.isInteger(ov) && ov >= 0 && ov < mc;

Try / catch

try { return chunk(maxChars, overlap); } catch (e) { if (String(e).includes("overlap must be")) { return chunk(maxChars, Math.floor(maxChars / 10)); } throw e; }

Prevention

When it happens

Trigger: Calling chunk(max_chars=1000, overlap=1000) or any overlap >= max_chars after the int/max(0,...) coercion, e.g. chunk(max_chars=100, overlap=500).

Common situations: Configuring overlap as a fraction but passing it as a raw value (0.5 vs half of max_chars); copying an overlap from a differently-sized chunking config; generated code that sets overlap equal to max_chars.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/8b1e78a70540015b. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/repl/runtime.rs:897

        snippet_start = max(0, start - 120)
        snippet_end = min(len(_context), end + 120)
        hits.append({
            "index": i,
            "start": start,
            "end": end,
            "match": m.group(0),
            "snippet": _context[snippet_start:snippet_end],
        })
    return hits

def chunk(max_chars=20000, overlap=0):
    """Return full-coverage input chunks with index/start/end/text fields."""
    max_chars = int(max_chars)
    overlap = max(0, int(overlap))
    if max_chars <= 0:
        raise ValueError("max_chars must be > 0")
    if overlap >= max_chars:
        raise ValueError("overlap must be smaller than max_chars")
    chunks = []
    start = 0
    idx = 0
    total = len(_context)
    while start < total:
        end = min(total, start + max_chars)
        chunks.append({"index": idx, "start": start, "end": end, "text": _context[start:end]})
        idx += 1
        if end >= total:
            break
        start = end - overlap
    return chunks

def chunk_context(max_chars=20000, overlap=0):
    """Compatibility alias for chunk()."""
    return chunk(max_chars=max_chars, overlap=overlap)

def chunk_coverage(chunks):

View on GitHub (pinned to 73e0f67d83)