TreeRAG is an embedding-free hierarchical retrieval system for runbook-style knowledge bases. It packages recursive tree building, cache-aware indexing, sibling-context retrieval, source-traceable answers, multi-document corpus routing, and a typed CLI/API into a production-ready Python project.
Provider support:
- OpenAI is the default backend.
- Gemini is also supported through the optional
geminiextra and CLI/API provider selection.
TreeRAG does not use embeddings or a vector database to navigate the index. It still uses LLM calls for segmentation, summarization, routing, and answer generation.
That means:
- retrieval is embedding-free
- indexing and querying are still model-dependent
- cost and latency are real tradeoffs, not hidden details
It is also not a strict keyword tree. Routing is summary-based and LLM-guided, so paraphrases and synonyms can still work. The bigger failure mode is weak summaries or overlapping sections that blur the right branch.
flowchart LR
DOC["Markdown / text document"] --> PARSE["Recursive segmentation"]
PARSE --> TREE["Typed document tree"]
TREE --> SUM["Bottom-up summaries"]
SUM --> CACHE["Content-hash cache"]
CACHE --> INDEX["JSON index with metadata"]
INDEX --> ROUTE["LLM-guided tree routing"]
ROUTE --> CTX["Sibling + ancestor context assembly"]
CTX --> ANSWER["Answer generation"]
- Typed public API:
build_index(...)andquery_index(...) - Typed corpus API:
build_corpus(...),load_corpus(...), andquery_corpus(...) - Benchmark APIs:
run_benchmark(...),run_comparison_benchmark(...),run_corpus_benchmark(...), andrun_corpus_comparison_benchmark(...) - CLI commands:
treerag index,treerag ask,treerag repl,treerag inspect,treerag corpus-index,treerag corpus-ask,treerag corpus-repl,treerag corpus-inspect,treerag benchmark,treerag compare,treerag corpus-benchmark,treerag corpus-compare - Provider selection through
--provider openai|gemini - Usage and cost estimates in benchmark JSON output
- Recursive parsing beyond depth two
- File-backed caches for segmentation and summaries
- Explicit routing errors instead of silent branch fallback
- Context assembly that can include nearby sibling leaves and ancestor summaries
- Line-aware source references for retrieved sections in markdown and text inputs
- Corpus manifests that route questions across multiple indexed documents before leaf selection
- UTF-8 JSON index storage with metadata and parent-link restoration
- Jira-style runbook example in
examples/jira_runbook.md
python3 -m venv .venv
.venv/bin/pip install -e .[dev]
export OPENAI_API_KEY=your_api_key_hereTo use Gemini instead of OpenAI:
.venv/bin/pip install -e '.[dev,gemini]'
export GEMINI_API_KEY=your_api_key_hereThe Gemini SDK currently targets Python 3.10+, so Gemini support should be used from a Python 3.10+ environment.
Build an index and ask one question:
treerag index examples/jira_runbook.md build/jira.index.json \
--cache-dir .cache/treerag
treerag ask build/jira.index.json "How do Sev-1 escalations work?" \
--sibling-window 1Example response:
{
"answer": "Page the primary on-call immediately and escalate after five minutes.",
"source_path": "examples/jira_runbook.md",
"selected_leaf_title": "Escalation Policy",
"selected_source_span": {
"start_line": 7,
"end_line": 9
},
"source_references": [
{
"title": "Severity Levels",
"start_line": 3,
"end_line": 5
},
{
"title": "Escalation Policy",
"start_line": 7,
"end_line": 9
},
{
"title": "Notification Rules",
"start_line": 11,
"end_line": 13
}
],
"navigation_path": [
"root",
"Incident Management",
"Escalation Policy"
]
}Build a routed corpus across multiple runbooks:
treerag corpus-index build/runbooks \
examples/jira_runbook.md \
examples/oncall_handbook.md \
examples/access_management_runbook.md \
--cache-dir .cache/treerag
treerag corpus-ask build/runbooks "Who coordinates responders during a Sev-1?" \
--sibling-window 1Stay in an interactive loop for follow-up questions:
treerag repl build/jira.index.json
treerag corpus-repl build/runbooksRun the same flow with Gemini-backed defaults:
treerag index examples/jira_runbook.md build/jira.gemini.index.json \
--provider gemini \
--cache-dir .cache/treerag
treerag ask build/jira.gemini.index.json "How do Sev-1 escalations work?" \
--provider gemini \
--sibling-window 1Build an index:
treerag index examples/jira_runbook.md build/jira.index.json \
--cache-dir .cache/treerag \
--subsection-threshold 200 \
--max-depth 4Ask a question:
treerag ask build/jira.index.json "How do Sev-1 escalations work?" \
--sibling-window 1Keep the same index open for repeated questions:
treerag repl build/jira.index.jsonInspect metadata:
treerag inspect build/jira.index.jsonBuild a routed corpus from multiple documents:
treerag corpus-index build/runbooks \
examples/jira_runbook.md \
examples/oncall_handbook.md \
--cache-dir .cache/treeragAsk the corpus a question:
treerag corpus-ask build/runbooks "Who owns Sev-1 response?" \
--sibling-window 1Keep the same corpus open for repeated questions:
treerag corpus-repl build/runbooksInspect the corpus manifest:
treerag corpus-inspect build/runbooksRun a benchmark suite:
treerag benchmark examples/jira_runbook.md benchmarks/jira_cases.json \
--index-path .cache/treerag/jira-benchmark.index.json \
--cache-dir .cache/treeragRun a corpus benchmark suite:
treerag corpus-benchmark build/runbooks \
benchmarks/operations_corpus_cases.json \
examples/jira_runbook.md \
examples/oncall_handbook.md \
examples/access_management_runbook.md \
--cache-dir .cache/treeragRun a side-by-side corpus comparison against simpler baselines:
treerag corpus-compare build/ops-compare \
benchmarks/corpus_comparison_cases.json \
examples/critical_outage_updates.md \
examples/oncall_handbook_compare.md \
--cache-dir .cache/treeragRun a side-by-side comparison against simpler baselines:
treerag compare examples/noisy_finance_report.md benchmarks/comparison_cases.json \
--index-path .cache/treerag/noisy-finance.compare.index.json \
--cache-dir .cache/treeragModel names are configurable from the CLI:
treerag index examples/jira_runbook.md build/jira.index.json \
--segmentation-model gpt-5.4 \
--summarization-model gpt-5.4-mini
treerag ask build/jira.index.json "Who gets paged first?" \
--routing-model gpt-5.4-mini \
--answer-model gpt-5.4When you pass --provider gemini, TreeRAG swaps to Gemini model defaults automatically unless you override them explicitly.
from treerag import (
GeminiProvider,
IndexConfig,
ModelConfig,
RetrievalConfig,
build_index,
query_index,
)
index = build_index(
"examples/jira_runbook.md",
"build/jira.index.json",
IndexConfig(cache_dir=".cache/treerag"),
model_config=ModelConfig(),
)
result = query_index(
"How do Sev-1 escalations work?",
"build/jira.index.json",
RetrievalConfig(sibling_window=1, include_ancestor_summaries=True),
model_config=ModelConfig(),
)
print(result.answer)
print(result.context)
print(result.source_references)
gemini_result = query_index(
"How do Sev-1 escalations work?",
"build/jira.index.json",
RetrievalConfig(sibling_window=1, include_ancestor_summaries=True),
model_config=ModelConfig(
routing_model="gemini-2.5-flash",
answer_model="gemini-2.5-flash",
),
provider=GeminiProvider(),
)Yes, when the document set is structured and the answer usually lives in one operational subsection. The Jira runbook example is a good fit because:
- headings naturally map to sections
- the answer can be traced back to concrete section lines instead of opaque retrieval scores
- neighboring subsections provide useful context
- the runbooks are small enough that hierarchical routing is cheaper than loading everything every time
It is a worse fit for:
- tiny documents where full-context prompting is simpler
- very large multi-document corpora without a stronger corpus-selection layer
- workloads where deterministic keyword or structured search already solves the task
Current local checks:
pytest tests -qruff check src testsmypy src testspython -m compileall src tests- manual checklist in
docs/manual-qa.md
GitHub Actions now runs the same gate set on pushes to main, pull requests, and manual workflow runs.
TreeRAG includes a lightweight benchmark harness for repeatable, question-based evals.
- Case files live in JSON and define expected leaf titles and answer substrings
treerag benchmarkmeasures index build time, total query time, and per-case resultsbenchmarks/jira_cases.jsongives the repo a concrete Jira-style benchmark targetbenchmarks/access_cases.jsoncovers access-control runbooks with approval and revocation flowsbenchmarks/appendix_cases.jsonprobes appendix-heavy and low-overlap questions against a finance-style report inexamples/finance_appendix_report.mdbenchmarks/comparison_cases.jsondrives side-by-side comparisons on a noisy finance report inexamples/noisy_finance_report.mdbenchmarks/corpus_comparison_cases.jsondrives corpus-level comparisons onexamples/critical_outage_updates.mdandexamples/oncall_handbook_compare.mdbenchmarks/paraphrase_cases.jsonprobes synonym and paraphrase-style questions against the same document structurebenchmarks/runbook_corpus_cases.jsonexercises corpus routing across multiple runbooksbenchmarks/operations_corpus_cases.jsonexpands corpus evals across incident, on-call, and access runbooks
You can run the appendix-style eval locally with:
treerag benchmark examples/finance_appendix_report.md benchmarks/appendix_cases.json \
--index-path .cache/treerag/finance-appendix.index.json \
--cache-dir .cache/treeragThat fixture is meant to pressure-test the exact retrieval story people usually bring up in finance and regulatory documents: the answer living in an appendix or supporting section that is structurally relevant even when the question wording does not closely mirror the target section text.
treerag compare is the next step beyond a plain benchmark. It runs:
tree_rag: the normal hierarchical retrieval pathkeyword_leaf: a simple lexical overlap baseline over indexed leavesfull_context: a no-retrieval baseline that dumps the whole document into the answer step
That gives you a concrete way to measure whether TreeRAG is actually helping on the same document and question set, instead of only reporting a single-method accuracy number.
If you want repeated-run timing and consistency sampling, add --repeat:
treerag compare examples/noisy_finance_report.md benchmarks/comparison_cases.json \
--index-path .cache/treerag/noisy-finance.compare.index.json \
--cache-dir .cache/treerag \
--repeat 5treerag corpus-compare does the same thing at the multi-document layer. It runs:
tree_rag: normal corpus routing plus normal tree retrieval inside the selected documentkeyword_document: a simple lexical document pick followed by normal tree retrieval inside that documentfull_context: a no-routing baseline that dumps the whole corpus into the answer step
That gives you a concrete way to test whether TreeRAG is actually adding value at the document-selection layer, instead of only winning after the right document has already been chosen.
--repeat works here too, and the JSON output now includes total_runs, query_samples_ms, and simple consistency flags for document choice, leaf choice, and answer text.
Benchmark outputs now also include usage snapshots plus build_cost_estimate, query_cost_estimate, and total_cost_estimate when the provider exposes token usage.
Title and answer matching now normalize punctuation, currency symbols, and whitespace before scoring, so cases like Appendix G - Debt Schedule vs Appendix G Debt Schedule and $61 million vs 61 million do not fail just because of formatting.
TreeRAG now includes a tracked live Gemini artifact under results/gemini/2026-04-03/README.md.
The first saved comparison run is results/gemini/2026-04-03/noisy_finance_compare.json. On that live run:
tree_ragrouted toAppendix G Debt Schedulekeyword_leafstayed inExecutive Summaryfull_contextblended the executive-summary claim with the appendix number- total estimated cost was
$0.0003347
That saved JSON was produced before the scorer normalization pass, so its passed flags are stricter than the current code. The raw output is still useful because it exposes the retrieval difference directly instead of only a pass/fail badge.
Built-in price estimates currently cover:
gpt-5.4gpt-5.4-minigpt-5.4-nanogemini-2.5-flashgemini-2.5-progemini-2.5-flash-lite
These estimates are based on the public provider pricing pages as of 2026-04-02.
TreeRAG also recognizes common dated and preview aliases such as gpt-5.4-mini-2026-03-17, gpt-5.4-nano-2026-03-17, gemini-2.5-flash-002, and gemini-2.5-flash-lite-preview-09-2025.
If you benchmark with another model, the JSON will still include token usage and will mark pricing as incomplete under missing_models.
The broader validation path is tracked in docs/validation-roadmap.md.
treerag corpus-index writes a small manifest plus one index per document:
build/runbooks/
├── corpus.json
└── documents/
├── jira-runbook.index.json
└── oncall-handbook.index.json
At query time, TreeRAG first routes into the right document summary, then performs normal tree navigation inside that document.