Course Content
LangChain Mastery
7 sections · 109 lessons
What is the RetrievalQA chain in LangChain?
What you need to know
What it looked like
1from langchain_classic.chains import RetrievalQA # was: from langchain.chains import RetrievalQA23qa = RetrievalQA.from_chain_type(4 llm=llm, retriever=retriever,5 chain_type="stuff", return_source_documents=True,6)7qa.invoke({"query": "What is the notice period?"})8# -> {"query": ..., "result": ..., "source_documents": [...]}The four chain types
chain_type | How it works | Trade-off |
|---|---|---|
stuff | Put all retrieved docs in one prompt | One call; fails if docs exceed the context window |
map_reduce | Answer per doc, then combine the answers | Many calls; scales to many docs |
refine | Answer from doc 1, then improve the answer with each next doc | Sequential and slow |
map_rerank | Answer per doc with a score, keep the best | Many calls; no combining across docs |
Today models have large context windows, so "stuff" (with a sensible k) covers most cases.
Why it was replaced
- The prompt was hidden inside the class; changing it meant special arguments.
- Output keys (
query,result) were fixed. - Streaming, batching and async were awkward compared with runnables.
The current equivalents
1from langchain_classic.chains import create_retrieval_chain2from langchain_classic.chains.combine_documents import create_stuff_documents_chain34combine = create_stuff_documents_chain(llm, prompt) # prompt must use {context}5rag = create_retrieval_chain(retriever, combine)6rag.invoke({"input": "What is the notice period?"})7# -> {"input": ..., "context": [Document, ...], "answer": "..."}These helpers are also in langchain-classic in 1.x. The fully current way is a few lines of LCEL (retriever | format_docs, prompt, model), or an agent with a retrieval tool when the model should decide whether to search. The point to make in an interview: all three do the same job; LCEL shows every step and needs no extra package.
A real-life example
A logistics company's support bot was written in 2023 with RetrievalQA. During the upgrade to LangChain 1.x, from langchain.chains import RetrievalQA fails with an import error, because chains moved out of the main package.
The team has two options. The quick fix is pip install langchain-classic and changing the import — the service is back in 10 minutes. The planned fix, done the next sprint, rewrites the chain in LCEL. That rewrite lets them add the "cite the shipment policy section" instruction to their own prompt, stream tokens to the chat widget (time to first word drops from about 3 seconds to under 1), and log the retrieved chunk ids for each answer.
Follow-up questions to expect
- "When would
map_reducestill make sense?" — When you must use many long documents that will not fit in one prompt, such as summarising 200 contracts. - "How do you return sources with LCEL?" — Keep the documents in the output:
RunnableParallel(context=retriever, question=RunnablePassthrough()).assign(answer=...). - "Is
langchain-classicsafe to depend on?" — It is maintained for compatibility, but new features go intolangchainandlangchain-core, so treat it as a migration bridge.