Moving Past Vector RAG to Tree and Graph Search
When you shred a 200-page financial filing into 512-token chunks, you destroy the document before the model even reads it. Here is how we engineered document intelligence around vision-first layout parsing, hierarchical tree & graph search, and diffusion-LM reasoning.
The original sin of semantic search RAG
When we started exploring retrieval-augmented generation (RAG) for real enterprise documents, almost every standard pipeline followed the exact same recipe: run OCR on a PDF, slice the raw text into 256- or 512-token chunks, embed those chunks into vectors, and run cosine similarity against a vector database.
That works fine for casual prose, but it falls apart the moment you feed it financial statements, multi-column contracts, or multi-year operational archives. Semantic vector search retrieves whichever chunks 'sound' similar in embedding space. A table row from Q3 2022 gets mixed up with a projection from Q3 2025 because both chunks share identical surrounding vocabulary. Column headers get severed from row values 40 lines below. Worse, whenever a user asks for a financial ratio, variance, or reconciliation across sheets, standard RAG asks the LLM to do arithmetic in prose—making every calculation hostage to the model's next-token guessing.
And when an auditor or analyst asks the simplest question of all—'Where exactly did this number come from?'—standard RAG can only wave vaguely at 'Chunk #47' rather than pointing to an exact page, table cell, and quoted phrase.
Where BM25, Hybrid RAG, and naive Graph RAG still hit a wall
Naturally, the industry tried patching vector search with additional retrieval layers. We built, tested, and benchmarked every one of them before realizing the bottleneck wasn't the similarity metric—it was the fact that the document's structure had already been destroyed.
BM25 & Lexical Search
BM25 excels at exact keyword, ticker, SKU, or clause-number matching where vector embeddings drift. But BM25 is completely blind to layout and hierarchy. If the term 'EBITDA' appears in a footnote, a summary paragraph, and three separate tables, BM25 scores flat bags of words without knowing which table cell belongs to which fiscal year or parent section.
Hybrid RAG (Dense Vectors + BM25 + Reciprocal Rank Fusion)
Combining dense semantic embeddings with sparse BM25 scores and reranking the top-k chunks improves surface recall, but it still operates on severed text chunks. If a 120-row schedule is split across six chunks, reranking those chunks doesn't reassemble the table or stop the model from blending historical figures with current ones.
Standard Graph RAG
Extracting entity-relation triples from flat text chunks builds a graph of concepts, which helps for broad thematic summaries. However, when triples are extracted from already-shredded chunks, the graph loses structural document hierarchy (Document → Section → Sub-table → Row/Cell). It turns quantitative tables into loose text edges and still cannot execute deterministic math.
How DocWorm parses layout—and how we ranked models for accuracy and speed
We realized that if you want deterministic answers out of a document, you have to preserve the geometry and structure the author gave it on page one. Instead of flattening pages into raw OCR strings, DocWorm's vision-first parser extracts full page layouts—identifying headers, nested sections, multi-column flows, footnotes, and coherent table grids that can be reused deterministically.
In our extraction benchmarks, our layout-preserving parser achieved significantly higher structural and tabular accuracy than standard fast OCR pipelines built around Gemini Flash-Lite, which frequently collapse complex merged headers or drift on dense financial grids.
Beyond layout parsing, we spent the past month benchmarking and ranking candidate models across the reasoning and synthesis pipeline. We deliberately traded raw RAG lookup speed for deeper model reasoning so answers would be properly verified and cited—and through our ranking evaluations, we found that using a diffusion language model (dLLM) offers the best of both worlds: high reasoning accuracy on structured evidence combined with parallel, high-throughput inference speed.
Hierarchical Tree Search and Cross-File Graph Search
Once a document is parsed with its layout intact, DocWorm never throws it into a flat chunk bucket. Instead, we index every file into two interlocking structures—a Hierarchical Document Tree and a Persistent Entity Knowledge Graph—stored in an isolated SQLite WAL database (`memory.db`).
Hierarchical Tree Search & Node Traversal
Every document becomes a navigable tree of nodes: Document → Chapter/Section → Page → Paragraph or Structured Table Node. When a query arrives, our retrieval coordinator matches table-of-contents titles, section headers, and table schemas first, then performs structure-guided node traversal—expanding parent nodes for context, sibling nodes for adjacent clauses, and neighboring pages so a multi-page table is always read as one coherent unit.
Cross-Document Entity Graph Search
Enterprise files rarely live in isolation. A cell in an Excel workbook references Schedule B on page 14 of a PDF contract. DocWorm links entities, defined terms, and metrics across every file in the corpus. Graph search acts as a structural map: one hop takes the agent from a spreadsheet row directly to the exact node in the PDF that governs it.
Coherent Tables & Sandboxed Code Execution
Because tables are retained as structured objects rather than orphan text, DocWorm never asks an LLM to do math in prose. For Excel workbooks and extracted tables, we pass only a compact structural schema (< 5KB, with zero raw cell values leaked into the prompt) to generate and run verified Python/pandas code inside an isolated sandbox—reconciling 100MB spreadsheets deterministically.
Our deliberate engineering trade-off
We consciously traded raw, sub-second vector-lookup speed for multi-step model reasoning and structure-guided traversal. In legal, financial, and operational workflows, waiting a few extra seconds for an answer that is mathematically verified and cited to the exact page number, table cell, and quoted phrase beats a fast hallucination every single time.
