0Pricing
LangChain / RAG / Vector DBs · Lesson

Parent Document and Sentence-Window Retrieval

Decouple the chunks you search from the chunks you return so the LLM gets precise matches with rich context.

Parent Document and Sentence-Window Retrieval is a free LangChain / RAG / Vector DBs lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the LangChain / RAG / Vector DBs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Chunk-Size Dilemma

Small chunks search precisely but lack context; large chunks give context but dilute relevance. Parent document retrieval resolves this tension by searching small and returning large.

Two Chunk Sizes

Index small child chunks for accurate similarity matching, but keep a link to the larger parent chunk that surrounds each one.

  • Search on child embeddings
  • Return parent text to the LLM

ParentDocumentRetriever

LangChain provides a ready-made retriever. You give it a child splitter, an optional parent splitter, a vector store, and a doc store for the parents.

from langchain.retrievers import ParentDocumentRetriever

retriever = ParentDocumentRetriever(
    vectorstore=vectorstore,
    docstore=store,
    child_splitter=child_splitter,
    parent_splitter=parent_splitter,
)

Adding Documents

The retriever splits each document into parents and children, embeds the children, and stores parents keyed by id so they can be fetched on a hit.

retriever.add_documents(docs)
results = retriever.invoke("What is the refund window?")
print(len(results[0].page_content))  # large parent text

Sentence-Window Retrieval

A variant indexes single sentences but, on retrieval, expands each hit to include the surrounding sentences. The model sees the exact match plus neighbors.

Storing the Window

During indexing you save the neighboring text in metadata so it can be stitched back at query time.

doc.metadata["window"] = " ".join(
    sentences[max(0, i-2): i+3]
)
doc.page_content = sentences[i]

Swapping Content After Search

After similarity search returns the matched sentence, replace its content with the stored window before passing it to the LLM.

for r in results:
    r.page_content = r.metadata["window"]

When to Use Each

Parent document suits structured docs with natural sections. Sentence-window suits dense prose where precise sentences matter most.

Avoiding Duplicate Parents

Multiple child hits can map to the same parent. Deduplicate by parent id so the LLM is not handed the same passage twice.

seen = set()
unique = []
for d in results:
    pid = d.metadata["parent_id"]
    if pid not in seen:
        seen.add(pid)
        unique.append(d)

Cost and Context Limits

Returning larger parents consumes more of the LLM context window. Balance the parent size against your token budget and the number of results k.

Putting It Together

Index fine-grained children, retrieve precisely, then expand to parents or windows. Your generation step receives focused yet contextual passages.

docs = retriever.invoke("cancellation terms")
context = "\n\n".join(d.page_content for d in docs)
answer = llm.invoke(f"Context:\n{context}\n\nQuestion: ...")

Quick Check

Test your understanding of decoupled retrieval.

Recap

You learned to decouple search and return units:

  • Parent document: search children, return parents
  • Sentence-window: match sentences, expand to neighbors
  • Deduplicate parents and watch context limits

Frequently asked questions

Is the “Parent Document and Sentence-Window Retrieval” lesson free?

Yes — the full text of “Parent Document and Sentence-Window Retrieval” is free to read here on the web, and the LangChain / RAG / Vector DBs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the LangChain / RAG / Vector DBs course, upgrade to CoddyKit PRO.

What will I learn in “Parent Document and Sentence-Window Retrieval”?

Decouple the chunks you search from the chunks you return so the LLM gets precise matches with rich context. You practise LangChain / RAG / Vector DBs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start LangChain / RAG / Vector DBs?

No prior experience is required. LangChain / RAG / Vector DBs on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Parent Document and Sentence-Window Retrieval” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this LangChain / RAG / Vector DBs lesson?

Yes. Every LangChain / RAG / Vector DBs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Multi-Query Retrieval Strategies
  2. Contextual Compression with LLMs
  3. Hybrid Search and Re-ranking
  4. Parent Document and Sentence-Window Retrieval
← Back to LangChain / RAG / Vector DBs