Sub topic 9
Decomposing Complex Questions into Sub-Questions
This chapter assumes you know how a basic RAG pipeline works (index, retrieve, generate), how embedding or BM25 search returns a top-k list, and how to call an LLM with a text prompt. The code is Python.
Why one query is not enough
Some questions contain several separate information needs. Consider: "How does the refund policy for annual plans differ from monthly plans, and which one covers accidental deletion?" A single search query has to match one region of embedding space. This question points at two or three different documents. The top-k results tend to favor one topic and miss the others, so the model answers from partial evidence.
Decomposition fixes this by splitting the question into sub-questions. Each sub-question is simple enough for one search to answer. You retrieve for each one, then combine the evidence and generate a single answer.
The basic pattern
- Ask the model to split the question into self-contained sub-questions.
- Run retrieval once per sub-question.
- Merge the passages, removing duplicates.
- Generate the answer from the merged context.
A working implementation
The function below asks for one sub-question per line and caps the number of them. The cap limits cost and stops the model from over-splitting.
from typing import Callable
def decompose(
question: str,
llm: Callable[[str], str],
max_sub: int = 3,
) -> list[str]:
prompt = (
f"Split the question below into at most {max_sub} sub-questions. "
"Each must be understandable on its own, with all names spelled out. "
"If the question is already simple, return it unchanged. "
"One per line, no numbering.\n\n"
f"{question}"
)
subs = [q.strip() for q in llm(prompt).splitlines() if q.strip()]
return subs[:max_sub] or [question]
def decomposed_search(
question: str,
llm: Callable[[str], str],
search: Callable[[str, int], list[str]],
top_k: int = 3,
) -> list[str]:
passages: list[str] = []
for sub in decompose(question, llm):
for passage in search(sub, top_k):
if passage not in passages:
passages.append(passage)
return passagesTwo details matter. The or [question] fallback means an empty model reply still yields a search. The instruction "understandable on its own" matters because a sub-question like "what about annual plans?" has the same missing-context problem as a follow-up in a conversation.
Merging: concatenate or fuse?
Sub-questions ask for different things, so their result lists do not compete. Concatenating with deduplication is usually right, as above. Reciprocal rank fusion suits multi-query expansion, where every query asks the same thing and agreement between lists is a signal. Here, agreement is not the goal. Coverage is.
Common errors
| Problem | Symptom | Fix |
|---|---|---|
| Over-splitting a simple question | Extra latency, noisy passages | Allow the model to return the question unchanged, and cap the count |
| Sub-questions that depend on each other | A later sub-question needs an earlier answer | Use multi-step retrieval instead (next chapter) |
| Lost constraints | A date or product name is missing from a sub-question | Tell the model to copy names and constraints into each one |
| Duplicate passages | The context fills with repeats | Deduplicate before generating |
When to choose decomposition
Use it when the question has several independent parts, or compares two things. Do not use it when the parts depend on each other, such as "Who founded the company that makes X, and where were they born?" The second part cannot be written until the first is answered. That case needs retrieval steps that use earlier results.
Exercise
A user asks, "Compare the free-tier rate limits of the Search API and the Upload API." Why does decomposition help more here than one search, and should you merge the results with reciprocal rank fusion?
Show answer
One query would likely match only one API's documentation, so the other API's limits would be missing. Decomposing into "What are the free-tier rate limits of the Search API?" and "What are the free-tier rate limits of the Upload API?" gives each part its own search. Concatenate with deduplication rather than fuse, because the two lists answer different questions and agreement between them carries no signal.
Exercise
Why should the decomposition prompt say each sub-question must be understandable on its own?
Show answer
Each sub-question is searched separately, with no access to the others or to the original question. A sub-question that relies on missing context, such as "and what about annual plans?", is a poor search query.