Python / Python Modern Generative AI and Agents Interview Questions
What is the Parent Document Retriever pattern and when does it improve RAG performance?
Standard RAG embeds large chunks (500–1000 tokens) to preserve context but stores them directly as the retrieved context. The trade-off: large chunks have better coherence but may score lower on retrieval similarity because their embedding averages out many ideas. Small chunks have precise embedding similarity but lack surrounding context.
The Parent Document Retriever solves this by splitting at two levels: small child chunks (50–200 tokens) are embedded for precise retrieval, but when a child chunk is retrieved, the full parent document (or larger parent chunk) is returned as context for the LLM. This combines the precision of small chunk retrieval with the coherence of large context windows.
from langchain.retrievers import ParentDocumentRetriever
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma
from langchain.storage import InMemoryStore
from langchain_community.document_loaders import PyPDFLoader
# Load documents
loader = PyPDFLoader('research_paper.pdf')
docs = loader.load()
# Parent splitter: large chunks preserved as context
parent_splitter = RecursiveCharacterTextSplitter(
chunk_size=2000, chunk_overlap=200
)
# Child splitter: small chunks for precise embedding retrieval
child_splitter = RecursiveCharacterTextSplitter(
chunk_size=200, chunk_overlap=20
)
# Vector store holds child chunk embeddings
vectorstore = Chroma(
collection_name='child_chunks',
embedding_function=OpenAIEmbeddings(model='text-embedding-3-small'),
)
# Doc store holds parent chunks by ID
docstore = InMemoryStore()
retriever = ParentDocumentRetriever(
vectorstore=vectorstore,
docstore=docstore,
child_splitter=child_splitter,
parent_splitter=parent_splitter,
)
# Index documents (stores parents in docstore, child embeddings in vectorstore)
retriever.add_documents(docs)
# At query time: retrieves by child similarity, returns parent chunks
results = retriever.invoke('What are the main findings?')
print(len(results[0].page_content)) # much larger than child chunk size
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
