Repository navigation
fix(rag): make LLM-based chunking terminate and fail loudly - #4262
Conversation
Indexing with use_llm_chunking=True looped forever on every document: the segment cursor rewound past the end of the last segment, and the LLM call used a method LemonadeClient doesn't have, so each pass silently fell back to word splitting and grew the chunk list without bound. - Stop after the final segment and guarantee the cursor always advances. - Call LemonadeClient.completions and send the whole segment, not the first 2,000 characters, so the model sees what it is asked to split. - Snap segment edges to whitespace and cap every chunk at chunk_size * 4 characters. - LLM or parse failures now raise with the segment range and model name; index_document reports that error instead of switching to the heuristic chunker behind the user's back. Fixes amd#4195
Request changesThis fixes two real bugs in LLM-based chunking — the loop that never ended, and the call to a Lemonade method that doesn't exist (so every "semantic" chunk was silently word-split). Both fixes are correct and the new tests are good. One regression came in with them. Chunk overlap is now effectively zero. The code aims the next segment 400 characters back (the configured 20% overlap) and then snaps to a word boundary — but it searches for that boundary in the wrong direction, landing right back at the end of the previous segment. Measured on a 15K-character document, the overlap kept is 7–8 characters instead of 400. That removes the context bridge between segments, which is exactly what Two smaller things: a chatty model that writes a citation-style Real-world evidenceNo evidence bundle was produced for this PR, and the description's two behavioural checks — the 🔍 Technical detailsIssues🟡 Overlap between segments collapses to ~0 characters (
Measured with the defaults ( The pre-PR line ( I ran this variant against the same document:
🟢 First bracketed integer wins the regex, including citations (
🟢 Sibling doc still implies the old fallback contract ( Part 2 got the "indexing fails with an error" note; part 3 recommends 🟢 Dead branch in the position filter ( The regex only matches integer literals, so Strengths
|
) Document and image extraction failed whenever the model wrote a placeholder such as `{field}` before its JSON answer: the parser only tried the first `{`, gave up, and the extraction raised "did not parse". It now moves past an invalid span and finds the real JSON. It still never looks inside a broken object, so truncated or malformed output returns nothing instead of a misleading fragment. Also adds table tests for the JSON parser behind every VLM extraction path, and tests that pin the Stable Diffusion prompt text to the real per-model defaults so the size, steps, and CFG the model is told to use can't drift from them. Covers the parser and SD-prompt parts of amd#4257. Chunking and cache-clearing are in amd#4262 and amd#4260. `deltas_cli`, `code_bench_fixtures`, and the TUI flag wiring already had tests. `talk/app.py` is left out because it needs a design decision (see the issue). ## Test plan - [x] `python -m pytest tests/unit/test_parsing.py`: 2 placeholder cases fail on `main`, 13/13 pass with the fix - [x] `python -m pytest tests/unit/test_parsing.py tests/unit/test_sd_prompts.py tests/unit/test_file_watcher.py tests/unit/test_structured_vlm_extraction.py tests/unit/test_vlm_extraction_failures.py tests/unit/test_sd_mixin.py tests/unit/test_sd_error_messages.py hub/agents/gaia/python/tests/test_sd_tools_do_not_rewrite_the_prompt.py`: 176 passed, 3 skipped - [x] black, isort, and pylint (errors-only, CI config) are clean on the changed files - [x] `python util/check_test_lane_coverage.py` does not flag either new file (the `pytest tests/unit/` lane runs them) Part of amd#4257 Co-authored-by: Kalin Ovtcharov <[email protected]>
Turning on LLM chunking (
gaia chat --use-llm-chunking, oruse_llm_chunking=True, which the deployment playbook recommends for max quality) hung indexing on every document. The loop never finished, and every pass quietly fell back to word splitting because the LLM call used a method the Lemonade client doesn't have. Now indexing finishes after one LLM call per ~6K-character segment. The model sees the whole segment it's splitting, and no chunk is longer thanchunk_sizetokens (about 4 characters per token). If the LLM can't be reached or returns no usable split list, indexing reports a clear error that names the model and the character range. It no longer switches strategy without telling you.Test plan
python -m pytest tests/unit/rag/test_llm_chunking.py -q: 9 passed. On main, 6 fail and the 2 failure-path tests hang forever.python -m pytest tests/unit/rag/ tests/test_rag.py tests/unit/test_rag_tools.py tests/unit/test_rag_index_status.py -q: 174 passedpython util/lint.py --black --isort: changed files are clean. isort flags 3 unrelated files that are already failing on main.pylint src/gaia/rag/sdk.pywith the CI config: cleangaia eval agent --category rag_quality --agent-type doc, compared against the committed baseline. The default path doesn't change (LLM chunking is off by default). Run this to confirm.gaia chat --use-llm-chunking --index <doc.pdf>against a running Lemonade Server finishes and answers from the document.Fixes #4195