AI / LangGraph LangChain Interview questions II
What are parent document retrieval patterns?
The parent document retrieval pattern addresses a fundamental tension in RAG systems: small chunks improve retrieval precision (the embedding closely matches the query), but large chunks provide richer context for the LLM to answer from. ParentDocumentRetriever resolves this by indexing small child chunks for search but returning their larger parent documents (or the full original documents) to the LLM.
from langchain.retrievers import ParentDocumentRetriever from langchain.storage import InMemoryStore from langchain_community.vectorstores import Chroma from langchain_openai import OpenAIEmbeddings from langchain.text_splitter import RecursiveCharacterTextSplitter # Child splitter: small chunks for precise retrieval child_splitter = RecursiveCharacterTextSplitter(chunk_size=400) # Parent splitter: larger chunks returned to LLM parent_splitter = RecursiveCharacterTextSplitter(chunk_size=2000) vectorstore = Chroma(embedding_function=OpenAIEmbeddings()) docstore = InMemoryStore() # stores parent documents retriever = ParentDocumentRetriever( vectorstore=vectorstore, docstore=docstore, child_splitter=child_splitter, parent_splitter=parent_splitter, ) retriever.add_documents(docs) # Query retrieves small child chunks but returns their 2000-char parents results = retriever.invoke("What is LCEL?")
This pattern significantly improves answer quality for knowledge-intensive tasks because the LLM receives enough surrounding context to reason about the answer, while the vector search remains precise.
More Related questions...