liveBook · Manning

Tree depth

As explained in Retrieval Augmented Generation, The Foundational Ideas

Max depth is a recursion limit that bounds the number of hierarchical levels generated by a tree-building process.

A maximum-depth cap bounds how many levels a recursive tree builder can generate. It is a safety guard, not necessarily the desired stopping point: ideally, clustering converges to one group and the builder writes a root that summarizes the whole corpus. If clustering keeps returning multiple groups, the cap prevents unbounded recursion and repeated summarization calls. section 10.4.4

Convergence before the cap

For twelve Cinderella passages, clustering produces 12 leaves, then 2 summaries, then 1 root. The tree stops when clustering converges, rather than because a target height was selected. section 10.4.4

Listing 10.6 The recursion that grows the tree upward.
def _summarize(self, members, level):
    text = self.summarizer.summarize_cluster(
        [n.text for n in members])
    node = RaptorNode(
        text=text, level=level,
        embedding=self.embed_model.embed_query(text),
        children={n.id for n in members})
    for child in members:
        child.add_parent(node.id)  #A
    self.all_nodes[node.id] = node
    return node
def _recursive_build(self, nodes, level, tree):
    if len(nodes) <= 1 or level > self.max_depth:  #B
        return
    embeddings = np.array([n.embedding for n in nodes])
    assignments = self.clusterer.perform_clustering(embeddings)
    clusters: Dict[int, List[RaptorNode]] = {}
    for idx, cluster_ids in enumerate(assignments):
        for c_id in cluster_ids:
            clusters.setdefault(c_id, []).append(  #C
                nodes[idx])
    tree[level] = [self._summarize(members, level)
                   for members in clusters.values()]
    if len(tree[level]) == 1:  #D
        return
    self._recursive_build(tree[level], level + 1, tree)

The builder checks the node count and depth before clustering; the cap stops further recursion when the next level would exceed the configured limit. section 10.4.4

Do not confuse a build-time depth cap with a traversal starting level: RAPTOR's tree_depth argument selects where retrieval descent begins, rather than limiting tree construction. section 10.5.2

How Retrieval Augmented Generation, The Foundational Ideas covers it

How deep it goes
Explains the cap alongside convergence and root creation, but does not specify a tuning procedure.
Examples
Uses twelve Cinderella passages to illustrate a 12-to-2-to-1 level progression.
What you can do after
Operate/configure a recursion guard that prevents tree construction from continuing indefinitely.
How it connects
Connects recursive tree building to clustering, summarization, embeddings, and tree traversal.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Retrieval Augmented Generation, The Foundational Ideas. The section links point to the parts of the book it draws on.