LangChain Mastery

Course Content

LangChain Mastery

7 sections · 109 lessons

What is the RetrievalQA chain in LangChain?


The same RAG job, old and currentRetrievalQA (legacy)• Prompt hidden inside the class• Fixed keys: query, result• chain_type picks stuff or map_reduce• Now imported from langchain_classicLCEL pipeline (current)• Your own prompt, visible in code• Returns context and answer together• Streams, batches and traces for free• Needs only langchain-core
Nothing about retrieval changed — only who owns the prompt and the output shape.

What you need to know

What it looked like

Python
from langchain_classic.chains import RetrievalQA   # was: from langchain.chains import RetrievalQAqa = RetrievalQA.from_chain_type(    llm=llm, retriever=retriever,    chain_type="stuff", return_source_documents=True,)qa.invoke({"query": "What is the notice period?"})# -> {"query": ..., "result": ..., "source_documents": [...]}

The four chain types

chain_typeHow it worksTrade-off
stuffPut all retrieved docs in one promptOne call; fails if docs exceed the context window
map_reduceAnswer per doc, then combine the answersMany calls; scales to many docs
refineAnswer from doc 1, then improve the answer with each next docSequential and slow
map_rerankAnswer per doc with a score, keep the bestMany 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

Python
from langchain_classic.chains import create_retrieval_chainfrom langchain_classic.chains.combine_documents import create_stuff_documents_chaincombine = create_stuff_documents_chain(llm, prompt)     # prompt must use {context}rag = create_retrieval_chain(retriever, combine)rag.invoke({"input": "What is the notice period?"})# -> {"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_reduce still 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-classic safe to depend on?" — It is maintained for compatibility, but new features go into langchain and langchain-core, so treat it as a migration bridge.