Inferensys

Glossary

Hypothetical Document Embeddings (HyDE)

A query expansion technique where a language model generates a hypothetical ideal document in response to a query, and the embedding of that synthetic document is used to search the vector store instead of the raw query.
Engineer reviewing vector database search results on laptop, embeddings visualization on screen, home office coding session.
QUERY EXPANSION TECHNIQUE

What is Hypothetical Document Embeddings (HyDE)?

A query expansion technique where a language model generates a hypothetical ideal document in response to a query, and the embedding of that synthetic document is used to search the vector store instead of the raw query.

Hypothetical Document Embeddings (HyDE) is a query expansion technique that uses a language model to generate a synthetic, ideal document in response to a user query, then encodes that generated document into a vector embedding for similarity search against a vector store. This bridges the semantic gap between short, ambiguous queries and the detailed, factual documents they aim to retrieve by aligning the query representation with the document embedding space.

The process involves prompting an instruction-tuned LLM to produce a plausible answer or document that directly addresses the query, even if factually incorrect. This hypothetical document captures the structural and linguistic patterns of real documents, and its embedding yields a more discriminative search vector than the raw query embedding alone, significantly improving recall in dense retrieval systems without requiring fine-tuned encoders.

HYPOTHETICAL DOCUMENT EMBEDDINGS

Key Characteristics of HyDE

HyDE (Hypothetical Document Embeddings) is a zero-shot retrieval technique that bridges the vocabulary gap between short queries and long documents by generating a synthetic 'ideal' answer before performing a vector search.

01

The Query-to-Document Gap

Standard dense retrieval embeds a short user query and searches for similar document chunks. This creates a vocabulary mismatch problem: queries use terse, keyword-heavy language, while documents use expansive, descriptive prose. Embedding both in the same space often fails to capture their semantic equivalence. HyDE solves this by first transforming the query into the document space.

  • Problem: Query 'What is a cat?' is short; relevant documents discuss 'feline domestication history'.
  • Result: Raw query embeddings often miss nuanced, long-form answers.
02

Synthetic Document Generation

HyDE instructs a generative language model (like GPT-3.5 or Llama 2) to create a hypothetical document that answers the user's query. The prompt is typically: 'Write a passage that answers the question: [QUERY]'. The model generates a synthetic text that captures the expected style, tone, and factual content of a real answer, even if the generated facts are hallucinated.

  • Key Insight: The synthetic document does not need to be factually correct.
  • Purpose: It only needs to 'look like' a real document to produce a useful embedding vector.
03

Embedding and Retrieval

The generated hypothetical document is then passed through a dense embedding model (e.g., Instructor-XL, Contriever) to produce a vector. This vector is used to query the vector database instead of the raw query embedding. Because the synthetic document mimics the structure of real indexed documents, its embedding lands in a dense region of the vector space populated by relevant, real documents.

  • Process: Query → LLM → Hypothetical Doc → Embedding Model → Vector → Vector DB Search
  • Contrast: Standard retrieval skips the LLM and Hypothetical Doc steps.
04

Zero-Shot and Language Agnostic

HyDE is fundamentally a zero-shot technique. It requires no fine-tuning, no labeled training data, and no model retraining. It works by chaining two pre-existing, off-the-shelf models: a generative LLM and a contrastively trained embedding model. Furthermore, because the generative model can produce text in any language it supports, HyDE is inherently language agnostic.

  • No Training: Works immediately with any generative model and any embedding model.
  • Multilingual: Generate the hypothetical document in the same language as the target corpus for cross-lingual retrieval.
05

Performance and Trade-offs

HyDE consistently outperforms standard dense retrieval (using the raw query embedding) on benchmarks like TREC DL and BEIR. It is particularly effective for complex, fact-seeking queries. The primary trade-off is latency and cost: a full LLM generation step is added before the vector search.

  • Latency: Adds 1-3 seconds for the LLM generation call.
  • Cost: Incurs token generation costs for every query.
  • Mitigation: Use smaller, faster instruction-tuned models (e.g., Llama-3-8B) to minimize overhead.
06

HyDE vs. Query2Doc

HyDE is closely related to Query2Doc, a contemporaneous technique. Both generate synthetic text to augment retrieval. The distinction is subtle: Query2Doc appends the generated pseudo-document to the original query before embedding, while classic HyDE replaces the query entirely with the hypothetical document's embedding.

  • HyDE: Embed only the hypothetical document.
  • Query2Doc: Concatenate query + generated doc, then embed.
  • Modern Practice: Many implementations now use a hybrid, embedding both and fusing results via Reciprocal Rank Fusion (RRF).
HYPOTHETICAL DOCUMENT EMBEDDINGS

Frequently Asked Questions

Core concepts and operational mechanics of the HyDE query expansion technique for improving retrieval accuracy in RAG systems.

Hypothetical Document Embeddings (HyDE) is a query expansion technique where a language model generates a hypothetical ideal document in response to a user query, and the embedding of that synthetic document is used to search the vector store instead of the raw query embedding. The process operates in two stages: first, a generative model like GPT-4 creates a plausible answer document—even if factually incorrect—that captures the structural and topical patterns of a relevant result. Second, an embedding model converts this synthetic document into a dense vector, which is then used to perform an approximate nearest neighbor (ANN) search against the vector database. This works because the hypothetical document's embedding resides closer to the actual relevant documents in the high-dimensional embedding space than the short, sparse query embedding would, effectively bridging the lexical gap between terse user queries and verbose indexed content.

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.