Inferensys

Glossary

Query Expansion with HyDE

A query expansion technique where a language model generates a hypothetical ideal document from a query, and the dense embedding of that generated text is used to perform a vector similarity search against a real document corpus.
Engineer reviewing vector database search results on laptop, embeddings visualization on screen, home office coding session.
HYPOTHETICAL DOCUMENT EMBEDDINGS

What is Query Expansion with HyDE?

Query Expansion with HyDE (Hypothetical Document Embeddings) is a retrieval technique where a language model generates a hypothetical ideal document from a user query, and the dense vector embedding of that synthetic text is used to perform a similarity search against a real document corpus, bridging the vocabulary gap between short queries and verbose documents.

The HyDE technique, introduced by Gao et al. in 2022, operates on the principle of query-document asymmetry. Short, keyword-based queries and detailed documents exist in different linguistic spaces, making direct embedding comparison suboptimal. HyDE instructs a generative language model, such as GPT, to fabricate a plausible, detailed document that would perfectly answer the query—even if the generated text contains factual errors. The critical insight is that the structure and topical vocabulary of this hypothetical document will closely mirror real relevant documents, creating a dense embedding vector that serves as a far more effective search probe than the original query's embedding.

The process follows a three-stage pipeline: generation, encoding, and retrieval. First, a prompt like 'Write a passage that answers the question' is fed to an instruction-tuned language model to produce the hypothetical document. Next, an unsupervised contrastively trained encoder transforms this synthetic text into a dense vector. Finally, this vector is used to query a vector database via approximate nearest neighbor (ANN) search. Crucially, HyDE is an unsupervised, zero-shot method requiring no relevance-labeled training data, making it immediately applicable to any domain. It is particularly effective for fact-seeking queries where the generated document's topicality compensates for its potential factual inaccuracy.

HYPOTHETICAL DOCUMENT EMBEDDINGS

Core Characteristics of HyDE

Hypothetical Document Embeddings (HyDE) represent a paradigm shift in query expansion, using generative language models to bridge the vocabulary gap between short user queries and the dense, factual language of indexed documents.

01

The Zero-Shot Grounding Mechanism

HyDE operates on a grounding-by-generation principle. Instead of directly embedding a short, ambiguous query, a language model first generates a hypothetical ideal document that answers the query. The dense embedding of this synthetic, detailed text is then used to perform a vector similarity search against the real document corpus. This process effectively re-anchors the query in the embedding space, moving it closer to the region where actual relevant documents reside, without requiring any task-specific training data.

02

Contrast with Traditional Query Expansion

Traditional query expansion methods, such as Pseudo-Relevance Feedback (PRF) or synonym expansion, add discrete terms to the original query. HyDE fundamentally differs by generating a complete, coherent passage. Key distinctions:

  • PRF assumes top-k initial results are relevant, risking query drift from bad initial hits.
  • Synonym expansion only adds words with similar meanings, failing to capture complex contextual relationships.
  • HyDE leverages the language model's parametric knowledge to synthesize a document that captures the query's intent in the same verbose, factual style as the target corpus, even if the generated text contains fabricated details.
03

The Role of the Generative Model

The instruction-tuned language model acts as a query-to-document translator. It is prompted with a simple instruction like, 'Write a passage that answers the question.' The model's output is not required to be factually correct; its purpose is to capture the linguistic pattern and topical density of a relevant document. This synthetic document serves as a dense, high-dimensional proxy for the user's information need, effectively performing a form of unsupervised domain adaptation for the retrieval system.

04

Unsupervised Corpus Alignment

A core advantage of HyDE is its unsupervised alignment with the target corpus. The generative model produces text in a verbose, explanatory style that naturally mirrors the statistical patterns of the indexed documents. This contrasts sharply with embedding a terse query directly, which often maps to a sparse, low-information region of the vector space. By generating a document-like embedding, HyDE performs a zero-shot transformation that aligns the query representation with the distribution of the document embeddings, significantly improving dense retrieval recall.

05

Instruction Following for Domain Control

The behavior of HyDE can be steered by modifying the instruction prompt given to the generative model. For a legal corpus, the prompt might be: 'Write a legal memorandum answering the question.' For a medical corpus: 'Write a section of a clinical guideline that addresses the topic.' This simple prompt engineering allows the same core architecture to adapt its generated document style to different domains, controlling the linguistic register and topical specificity of the hypothetical document to better match the target index.

06

Re-ranking with HyDE

While HyDE is primarily a retrieval-stage technique, its generated document can also be used for re-ranking. The synthetic passage can be fed alongside a candidate document into a cross-encoder model to compute a fine-grained relevance score. This provides a powerful signal because the cross-encoder can directly compare the generated 'ideal' answer with the actual retrieved document, assessing factual overlap and logical consistency beyond simple vector similarity.

QUERY EXPANSION WITH HYDE

Frequently Asked Questions

Explore the mechanics, benefits, and limitations of Hypothetical Document Embeddings (HyDE), a novel technique that uses generative language models to bridge the vocabulary gap between short user queries and the rich semantic content of indexed documents.

Query Expansion with Hypothetical Document Embeddings (HyDE) is a zero-shot dense retrieval technique where a generative large language model (LLM) first creates a hypothetical ideal document in response to a user query, and the dense vector embedding of that generated text is then used to perform a similarity search against a real document corpus. The core mechanism operates in four steps: first, a user query like 'how to mitigate transformer hallucination' is passed to an instruction-tuned LLM with a prompt to 'write a scientific passage that answers the question.' Second, the LLM generates a synthetic document, which may contain factual errors but captures the correct topical essence and linguistic patterns. Third, an encoder model like a Contriever or text-embedding-3-large converts this hypothetical document into a dense embedding vector. Finally, this vector is used to query a vector database via approximate nearest neighbor (ANN) search, retrieving real documents that are semantically similar to the hypothetical one. This process effectively transforms a sparse, keyword-deficient query into a dense, context-rich representation that aligns more closely with the embedding space of the indexed corpus.

COMPARATIVE ANALYSIS

HyDE vs. Other Query Expansion Techniques

A technical comparison of Hypothetical Document Embeddings against alternative query expansion methodologies for dense retrieval pipelines.

FeatureHyDEPseudo-Relevance FeedbackSynonym ExpansionLLM Query Rewriting

Core Mechanism

Generates hypothetical document from query, embeds it for vector search

Extracts terms from top-k initial retrieval results

Adds dictionary or thesaurus synonyms to original terms

Uses LLM to reformulate or decompose original query

Retrieval Paradigm

Dense vector only

Sparse or hybrid

Sparse keyword

Dense or hybrid

Requires Initial Retrieval Pass

Handles Vocabulary Mismatch

Strong: bridges semantic gap via generation

Moderate: limited to terms in top-k docs

Weak: only exact synonym matches

Strong: can introduce new semantic concepts

Latency Overhead

High: one LLM generation + one embedding inference

Medium: two full retrieval passes

Low: dictionary lookup

High: one LLM generation call

Risk of Topic Drift

Moderate: hallucinated document may diverge

High: noise from irrelevant top-k docs compounds

Low: synonyms preserve core meaning

Moderate: rewriting may alter original intent

Cold Start Performance

Strong: no prior relevance data needed

Weak: depends on quality of initial retrieval

Strong: relies on static lexical resources

Strong: zero-shot capability of LLM

Computational Cost

High: generative model inference

Medium: two retrieval passes

Negligible

High: generative model inference

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.