Query rewriting: detect when the rewrite changes intent
In this article (5 sections)
Query rewriting can add synonyms and remove conversational noise, but it can also drop a negation, alter a number or generalize an identifier. Retrieval may improve for the wrong question.
Check protected elements
The retrieval evaluation lab stores three authored rewrites.
from retrieval_cases import rewrite_case
result = rewrite_case()
assert result["protected_elements"] == ["negation", "numbers", "identifiers"]
assert result["rejected_indices"] == [1]
assert result["rewrite_model_executed"] is False
print(result["rows"])The rewrite of “travel not above 15000 INR” drops not and is rejected. The other examples preserve amount or order ID. No rewriting model ran.
Define an intent contract
Extract protected values from the original: named entities, dates, units, numbers, quoted phrases, negation, user/tenant scope and requested time. Require them in the rewrite unless a reviewed normalization maps them explicitly.
Keep the original query alongside rewritten variants. Retrieve with both or fall back to the original when validation fails. Never let a rewrite broaden authorization or add actions the user did not request.
Evaluate downstream effect
Label whether each rewrite preserves intent and whether it improves retrieval. Track four outcomes: preserved/helped, preserved/no gain, changed intent, and invalid/empty. A higher recall score cannot excuse a critical intent change.
Test short ambiguous queries, acronyms, multilingual text, IDs, exclusions and comparisons. Human review is especially useful where paraphrase equivalence is subjective. Version the rewriter and its validation rules.
The Generative & Agentic AI course connects rewriting to query traces, fixed eval sets and bounded agent behaviour.
Exercise
Create 40 original/rewrite pairs and label intent preservation independently. Add deterministic checks for numbers, IDs and negation, then compare retrieval gains only among accepted rewrites.
Continue learning
This article is part of the Retrieval quality and grounded answers sequence. Use the neighbouring tasks when you need the prerequisite or the next application.
- Review the prerequisite or neighbouring task in Reranking: measure the quality and latency trade-off.
- Continue with Multi-query retrieval with bounded cost.
Reference: NIST Generative AI Profile.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our Generative & Agentic AI programme — 3 months. Add practical GenAI, retrieval and agent-building skills to your existing toolkit.
Explore Generative & Agentic AI