A tree organizes entities or text into nested levels, replacing a flat list with relationships between leaves, intermediate summaries, and a root. In hierarchical retrieval, this structure preserves context across granular and broad questions. section 10.0 section 10.2
A recursive retrieval tree is built from the bottom up:
- Leaf nodes retain original chunks for specific factual questions. section 10.2
- Cluster nodes group semantically similar chunks and store LLM-written summaries. section 10.2
- Higher layers repeatedly group and summarize lower layers until a root summarizes the corpus. section 10.2
Choosing the retrieval level
A query asking for a contract's specific termination fee can match a leaf chunk, while a query about broad termination conditions can match a summary node. This reduces incorrect specificity and incomplete answers caused by flat retrieval. section 10.0 section 10.2
Listing 10.1 RaptorNode.
import uuid
from typing import List, Optional, Set, Dict, Any
class RaptorNode:
"""A single node in the RAPTOR tree."""
def __init__(self, text: str, level: int,
embedding: List[float],
children: Optional[Set[str]] = None,
metadata: Optional[Dict[str, Any]] = None):
self.id = str(uuid.uuid4()) #A
self.text = text #B
self.level = level #C
self.embedding = embedding #D
self.children = children if children else set() #E
self.parents = set() #F
self.metadata = metadata if metadata else {} #G
def add_child(self, child_id: str):
self.children.add(child_id)
def add_parent(self, parent_id: str):
self.parents.add(parent_id)
def is_leaf(self) -> bool:
return self.level == 0 and len(self.children) == 0Use the RaptorNode listing to inspect node text, hierarchy level, embeddings, children, parents, and leaf detection. Soft clustering can give one node multiple parents, making the implementation a directed acyclic graph rather than a strict tree. section 10.4.1













