RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /LangChain for Local AI
  6. /Ch. 15
LangChain for Local AI

15. RetrievalQA Chain

Chapter 15 of 18 · 20 min
KEY INSIGHT

`stuff` works for <4 documents, `map_reduce` for large batches, and `refine` when document order matters—choose based on document count and whether order affects meaning.

RetrievalQA chains come in multiple types controlling how retrieved documents combine with the query. Understanding the tradeoffs prevents expensive mistakes in production.

stuff mode concatenates all retrieved documents into one prompt. Fastest, cheapest, but fails if combined documents exceed context length.

qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=retriever,
    chain_type="stuff",
    prompt=prompt
)

map_reduce sends each document separately to the LLM, gets individual answers, then combines them. Handles any number of documents but makes N+1 LLM calls (one per document plus one for final answer).

qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=retriever,
    chain_type="map_reduce",
    map_prompt=PromptTemplate.from_template("""Answer this based on the document:

Question: {question}

Document: {context}"""),
    combine_prompt=PromptTemplate.from_template("""Combine these partial answers:

{answers}

Original question: {question}

Final answer:""")
)

refine processes documents sequentially, updating its answer as it sees each new document. Better for ordering-sensitive content like procedures or narratives.

qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=retriever,
    chain_type="refine"
)

For debugging, inspect intermediate steps.

from langchain.chains import RetrievalQA

# Enable verbose to see retrieval steps
qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=retriever,
    chain_type="stuff",
    return_intermediate_steps=True
)

result = qa_chain.invoke({"query": "your question"})
print("Intermediate steps:", result["intermediate_steps"])

Common failure: chain_type="refine" with contradictory documents produces unstable results. The model flips between conflicting answers as each new document arrives.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Run the same query with stuff, map_reduce, and refine. Compare response times and answers to understand when behavior differs.

← Chapter 14
Simple RAG Pipeline
Chapter 16 →
Streaming with LangChain