Documentation Index
Fetch the complete documentation index at: https://mintlify.com/avnlp/dspy-opt/llms.txt
Use this file to discover all available pages before exploring further.
QueryRewriter is a dspy.Module that transforms raw user queries into clean, search-optimized strings before they reach the retrieval layer. It expands relevant synonyms and concepts, clarifies ambiguous terms, removes conversational noise, and preserves critical constraints such as dates, numbers, and named entities. Because it is a standard DSPy module, it is fully optimizable — DSPy optimizers like MIPROv2 and COPRO can tune its prompt instructions and few-shot demonstrations automatically.
Signature
The module is driven byQueryRewriteSignature, which defines the contract between the LLM and the rewriting task:
| Field | Type | Description |
|---|---|---|
original_query | InputField | The user’s raw search query. The LLM must rewrite it to improve search effectiveness without altering core intent — expanding synonyms, clarifying ambiguity, removing noise, and preserving key entities and constraints. |
rewritten_query | OutputField | Optimized query string ready for the search engine. Must be 5–15 words, contain only essential search terms, exclude phrases like “I want” or “looking for”, and output only the rewritten query string with no additional text. |
Constructor
When
True, the rewriter uses dspy.ChainOfThought to reason through the transformation step by step before producing the rewritten query. This yields higher-quality rewrites at the cost of a slightly longer LLM call. When False, it uses dspy.Predict for a direct, faster rewrite with no explicit reasoning trace.Methods
forward
The original user search query to rewrite.
dspy.Prediction containing:
rewritten_query— the optimized search stringrationale(only whenuse_chain_of_thought=True) — the step-by-step reasoning used to produce the rewrite
batch_rewrite
forward on each. Returns a plain List[str] of optimized query strings in the same order as the input.
A list of original user search queries to rewrite.
List[str] — one optimized query string for each input query.
Example transformations
The module applies several classes of transformation derived from the signature instructions and class docstring:| Original query | Rewritten query |
|---|---|
"How do I fix my leaking faucet?" | "faucet repair leak plumbing tools" |
"Best camera under $500 for travel" | "best compact camera $500 travel" |
"cheap flights to Paris next week" | "affordable flights Paris France departure date next 7 days" |
Usage
DSPy optimization
QueryRewriter is a first-class DSPy module — it stores its predictor (self.rewriter) as a named attribute that DSPy optimizers can inspect and modify. This means you can compile it with any DSPy optimizer to automatically improve its prompt instructions and few-shot examples against your retrieval quality metric.
After compilation, save the optimized module with
compiled_rewriter.save("rewriter.json") so you don’t need to re-run the optimization on every startup.