Sub topic 4
Turning Follow-up Questions into Standalone Queries
This chapter assumes you have a working RAG pipeline (index, retrieve, generate), know what embeddings and BM25 do, can call an LLM with a text prompt, and can read Python.
The problem: follow-ups depend on context
A user asks, "What is the Acme X200 router?" and gets a good answer. Then they ask, "What about its pricing?" A person reads this in context. A search index does not. It sees the words "what about its pricing" and has no way to know the subject is the X200.
Sending the follow-up to retrieval as-is usually fails in one of two ways:
- Pronouns and references ("its", "that one", "the second option") carry no searchable content, so the retriever returns generic pricing pages or nothing useful.
- Dropped constraints. If the user said "for the EU region" three turns ago, that constraint is missing from the query.
The fix is to rewrite the follow-up into a standalone query: a question that can be understood with no conversation attached. Here "What about its pricing?" becomes "What is the pricing of the Acme X200 router?"
Where the rewrite sits
The rewrite happens before retrieval. The original follow-up is still shown to the user and kept in the history. Only the search sees the rewritten text.
Implementation
The function below takes the history as a list of (role, text) pairs and an llm callable that maps a prompt string to a reply string. With no history, it returns the question unchanged and makes no model call.
from typing import Callable
def condense_question(
history: list[tuple[str, str]],
follow_up: str,
llm: Callable[[str], str],
) -> str:
if not history:
return follow_up
transcript = "\n".join(f"{role}: {text}" for role, text in history)
prompt = (
"Rewrite the final user question so it can be understood without "
"the conversation. Keep all names and constraints. "
"Return only the rewritten question.\n\n"
f"{transcript}\nuser: {follow_up}"
)
return llm(prompt).strip()Three details in the prompt matter:
- "Keep all names and constraints" stops the model from dropping the region, version, or date the user mentioned earlier.
- "Return only the rewritten question" keeps explanations out of the search string. Extra text such as "Sure! Here is the rewritten question:" would pollute the query.
- The transcript ends with the follow-up, so the model sees which turn to rewrite.
Call it like this, with any model client behind llm:
history = [
("user", "What is the Acme X200 router?"),
("assistant", "The X200 is a Wi-Fi 6 router for small offices."),
]
query = condense_question(history, "What about its pricing?", llm)
# Expected shape: "What is the pricing of the Acme X200 router?"Common errors
- Rewriting questions that are already standalone. The model may add details the user never asked about. The early return for empty history helps, but a mid-conversation topic change is also a risk. Tell the model to return the question unchanged if it already makes sense on its own.
- Answering instead of rewriting. Some models respond to the question. Check that the output is short and question-shaped, or fall back to the original follow-up.
- Sending too much history. Long transcripts raise cost and can pull in old topics. Keep the last few turns, and truncate long assistant answers.
- Losing the topic switch. If the user changes subject, old context can leak into the query. This is an early form of query drift, covered later in this tutorial.
Check the output
Log the original follow-up, the rewrite, and the retrieved document ids together. When retrieval looks wrong, the rewrite is the first thing to read. A bad rewrite is easy to spot and cheap to fix by editing the prompt.
Exercise
A user asks "How do I install the CLI?" and gets an answer. They then ask "And on Windows?" Why does sending "And on Windows?" directly to the retriever perform poorly, and what should the condensed query contain?
Show answer
The retriever sees only "And on Windows?", which has no subject, so it matches pages about Windows in general rather than the CLI. The condensed query should restore the subject and the earlier intent, for example "How do I install the CLI on Windows?"
Exercise
Why does condense_question return the follow-up unchanged when history is empty?
Show answer
With no earlier turns, there is nothing to resolve, so the question is already standalone. Skipping the model call saves latency and cost and removes the risk of the model altering a good query.