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.
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
- Take one section of a doc as the parent.
- Split it into 5 children.
- 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.
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]])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.