Skip to content

Query Transformation in RAG: Coreference Resolution, Multi-Query Expansion, and HyDE

2 min read

Users rarely type clean, self-contained search queries into a chat interface. They send fragmented follow-ups ("what about the yearly option?", "does it work in Kazakhstan?", "why did it fail?") or ask compound questions spanning two separate documents. Passing the raw last message straight into retrieval is one of the fastest ways to lose recall.

Four High-Value Query Transformations

TechniqueInput ExampleTransformed OutputBest Use Case
Coreference Rewriting"Does it include refunds?" (after asking about Pro plan)"Does the Pro plan include refunds?"Multi-turn support and sales chat
Multi-Query Expansion"Connect existing WhatsApp number safely"3 phrasings covering Coexistence, Cloud API, migrationBroad conceptual questions with vocabulary mismatch
Query Decomposition"Compare Pro vs Enterprise limits and SSO support"Sub-query 1: plan limits; Sub-query 2: SSO availabilityMulti-hop comparison questions
HyDE (Hypothetical Doc)"Why are outbound templates rejected?"Short synthetic answer paragraph embedded for dense searchAbstract questions where query style differs from docs

Guarding Against Query Drift

Every rewrite step introduces latency and a risk of hallucinating constraints the user never stated. Keep the rewriting prompt narrow, deterministic, and strictly grounded in the recent conversation window:

ts
export const QUERY_REWRITE_SCHEMA = {
  type: "object",
  additionalProperties: false,
  required: ["standaloneQuery", "needsRetrieval"],
  properties: {
    standaloneQuery: {
      type: "string",
      description: "Self-contained search query preserving user language and exact codes.",
    },
    needsRetrieval: {
      type: "boolean",
      description: "False for greetings, acknowledgments, or pure small talk.",
    },
  },
} as const;

Latency and Cost Discipline

  • Always preserve original identifiers: instruct the rewriter never to paraphrase error codes, SKUs, phone numbers, or order IDs.
  • Skip retrieval on non-informational turns: classifying needsRetrieval: false for "thanks!" or "ok" saves embedding calls, vector queries, and prompt tokens.
  • Run original + rewritten queries in parallel when uncertain: fusing results from both the raw turn and the rewritten query via RRF protects against over-eager rewrites.

Next: narrow 30 fused candidates down to the sharpest context window in Reranking, MMR Diversity, and Context Compression.

Was this helpful?