Area 02 · RAG · Concept 14 of 20

Parent-Child Chunking

Search small chunks for precision, then send their bigger parent section for context.

11–20 · Production levelPhase 2 · Weeks 7–10≈ 45 min

The idea

Small chunks match precisely but carry little context. Large chunks carry context but match vaguely. Parent-child chunking does both: split documents into parent sections (for example 2,000 tokens), split each parent into small children (for example 200 tokens), and embed only the children.

At query time you search the children, then look up their parents and send the parent text to the model. Deduplicate when several children share one parent.

A related idea is sentence-window retrieval: match a single sentence, then include a few sentences on either side.

Why it matters

It improves answers to questions that need surrounding detail, without hurting search precision.

Words to know

  • Parent / child chunkLarge context unit / small search unit that points to it.

Try it without code

Map children to parents

  1. Take one section of a doc as the parent.
  2. Split it into 5 children.
  3. For a question, find the best child and see whether the parent gives a fuller answer.

Use it in code

Retrieve children, return unique parents.

python
def retrieve_parents(conn, qvec, k_children=12, k_parents=4):
    rows = conn.execute("""
        SELECT parent_id, embedding <=> %s AS dist
        FROM child_chunks ORDER BY dist LIMIT %s""", (qvec, k_children)).fetchall()
    seen, parent_ids = set(), []
    for pid, _ in rows:
        if pid not in seen:
            seen.add(pid); parent_ids.append(pid)
    parents = conn.execute("SELECT id, body FROM parent_chunks WHERE id = ANY(%s)",
                           (parent_ids[:k_parents],)).fetchall()
    order = {pid: i for i, pid in enumerate(parent_ids)}
    return sorted(parents, key=lambda p: order[p[0]])
PRACTICE · TICK IT AFTER THIS

Add parent and child tables to your pipeline and compare answer completeness on 10 questions that need context.

Common mistakes

  • Parents so large they blow the token budget.
  • Not deduplicating parents.

Go deeper