Query Contextualization with Conversation History
What You'll Learn
You'll learn how to rewrite user queries by incorporating relevant context from previous conversation turns, enabling your RAG system to understand follow-up questions and pronouns correctly.
The Problem with Standalone Queries
Imagine a user asking: "Tell me about vector databases," followed by "What are their main advantages?"
That second query—"What are their main advantages?"—is ambiguous on its own. The pronoun "their" has no meaning without the conversation history. If you feed this raw query to your retriever, it won't know what "their" refers to, leading to poor or irrelevant retrieval results.
How Query Contextualization Works
Query contextualization rewrites each new user query into a self-contained, standalone query by pulling in relevant information from the conversation history.
Example transformation:
- Original query: "What are their main advantages?"
- Contextualized query: "What are the main advantages of vector databases?"
This process typically uses an LLM with a simple prompt like:
Given the conversation history and the latest user question,
rewrite the question to be standalone and self-contained.
History: [previous messages]
Latest question: "What are their main advantages?"
Standalone question:
The LLM understands the conversation flow and produces a query that your retriever can actually work with.
When to Apply It
Use query contextualization when:
- Building conversational RAG systems (chatbots, assistants)
- Users ask follow-up questions with pronouns or references
- Context from 2-3 previous turns is usually sufficient
You can skip it for the first query in a conversation—there's no history yet!
Key Takeaway: Query contextualization transforms context-dependent questions into standalone queries by incorporating conversation history, dramatically improving retrieval accuracy in multi-turn conversations.