Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 32/ 160
02. RAG

HyDE

Hypothetical Document Embeddings: generate a fake answer, embed it, then retrieve real passages.

AdvancedHypothetical Doc · Embed Answer3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

The model first guesses an answer, even if it's imperfect, and the system searches for real documents that look like that guess.

Think of it like…

To find a book on a shelf, you first sketch what its page might look like, then look for pages that resemble your sketch. The sketch may contain wrong details, but it looks more like the target than your one-line question does.

Where you’ve seen it

A research assistant over biomedical papers handles queries like "does intermittent fasting improve insulin sensitivity?". Embedding the question retrieves opinion articles and diet blogs. With HyDE, the LLM writes a short abstract-style paragraph ("Studies in adults with prediabetes report improved fasting insulin and HOMA-IR after 8 to 12 weeks of time-restricted eating…"), whose embedding is much closer to real trial abstracts. Recall@10 against curated relevant papers rises from 0.58 to 0.71, with 600 ms of extra latency.

Words you’ll hear

Hypothetical document
An LLM-generated passage answering the query, used only to build a search vector.
Query-document gap
The mismatch in style and length between short questions and long answer passages.
Zero-shot retrieval boost
Improvement without training or labelled relevance data.
Query + HyDE fusion
Combining the question's vector with the hypothetical document's vector or results.

Visual diagram

Loading interactive diagram

Theory & deep dive

Why it works

Dense retrievers are trained to map queries near relevant passages, but in practice a question and its answer often differ in length, vocabulary and form. A generated pseudo-answer reuses the vocabulary and structure of real answers, with domain terms the user didn't know to include, so its embedding sits closer to the target region. The LLM acts as a translator from "how people ask" to "how documents are written". Because only the embedding is used for search, factual errors in the hypothetical text matter much less than its style and topic, as long as it stays on topic.

The original HyDE paper (2022) showed gains over unsupervised dense retrievers on web search and low-resource tasks. Modern strong embedding models, trained on large question-passage data, narrow the gain on well-formed queries, so the benefit is largest for short, vague, jargon-light queries against technical or specialist corpora.

A HyDE prompt

"Write a short passage (3-4 sentences) from a technical document that directly answers the question. Use precise domain terminology. Question: {query}" Embed the output (or average it with the query embedding) and retrieve.

Key takeaway: A fake answer looks more like real passages than a question does.

Costs, failure modes and alternatives

HyDE adds a generation step (hundreds of milliseconds and some tokens) before retrieval, so use a small fast model, cache results for repeated queries and apply it selectively, for example only when first-stage confidence is low or the query is short and abstract. Failure modes: the model hallucinates a plausible but wrong topic and steers retrieval away; it paraphrases away exact identifiers, so keep BM25 on the original query; for queries about private facts the model can't know ("what did we decide in the Q3 pricing review?"), the hypothetical document is generic and may retrieve generic material.

Make it safer by fusing: retrieve with the original query and with the HyDE embedding, then combine with RRF, and rerank. Compare with related methods: query rewriting (clearer query, not a fake answer), contextual retrieval (improves the documents side), and fine-tuning the embedding model on your question-passage pairs, which often gives larger and cheaper-at-runtime gains if you have data. Always A/B on labelled queries before adopting.

Selective use

Run HyDE only when the query has fewer than 8 words or the top first-stage cosine score is below a calibrated threshold. On the test set this keeps the recall gain on hard queries while avoiding extra latency on 70% of traffic.

Key takeaway: Use selectively, fuse with the original query, and measure against rewriting and fine-tuning.

How it works, step by step

Step 1 of 4
Analysis

1. Identify the gap

Check whether failures come from short or abstract queries that don't resemble the documents.

recall@10 for short queries: 0.58
Key numbers at this step
Baseline
measured first

When to use it & trade-offs

What it is

HyDE (Hypothetical Document Embeddings) retrieves with a made-up answer instead of the question. An LLM writes a plausible passage that answers the query (it may contain errors), that passage is embedded, and the vector is used to search. Questions and answers look different in embedding space (short, interrogative versus long, declarative), while a generated answer looks like the documents you want to find, so it often lands closer to the real passages. The hypothetical text is discarded; only real retrieved documents reach the final answer.

Why it matters: Short, abstract or poorly worded queries embed poorly: "why does my build fail on CI but not locally?" shares little surface form with a troubleshooting page about environment differences. HyDE can raise recall without labelled data or fine-tuning, a useful trick when the question-to-document gap is large. It also has costs and failure modes, so knowing when not to use it matters as much as how it works.

When to reach for it

  • Specialist domains with jargon users don't know
  • Short, abstract questions with low first-stage confidence
  • Quick zero-shot recall improvements

When to avoid or be careful

  • HyDE for exact identifiers, codes and names
  • Using it on every query without measuring latency and gain
  • Presenting the hypothetical text to users as fact

Ways to close the query-document gap

HyDE
Best for: Short or abstract queries over specialist corpora; no training data

Strengths: Zero-shot recall boost; no index changes.

Watch out: Extra generation latency and cost; can hallucinate off-topic.

Query rewriting / expansion
Best for: Follow-ups, vague or vocabulary-mismatched queries

Strengths: Cheaper; keeps queries interpretable.

Watch out: Doesn't imitate document style.

Fine-tuned embeddings
Best for: Stable domains with question-passage data

Strengths: No run-time generation; large gains.

Watch out: Needs data and training; re-embedding.

Interview signals & numbers to know

When this topic comes up

  • “Short queries retrieve poorly”
  • “What is HyDE?”
  • “Questions don't look like documents”
  • Zero-shot retrieval improvements
  • Query-document mismatch

Benchmarks & rules of thumb

+1 LLM call -
added per query
0.58 → 0.71 -
recall@10 gain in the example
2022 -
year of the HyDE paper

Common mistakes & follow-ups

Common interview mistakes

  • Embedding only the hypothetical document and losing exact terms that were in the question.
  • Letting the LLM answer from memory and showing that text to users.
  • Applying HyDE everywhere and doubling latency for queries that were already retrieved well.

Likely interviewer follow-ups

  • Why would retrieving with a fake answer work better than the question?
  • When would you avoid HyDE?
  • How does HyDE compare with fine-tuning the embedding model?
Interview tip: Explain it as translating "how people ask" into "how documents are written", then give two cases where you'd skip it. Knowing limits makes the answer credible.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply HyDE with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Query Rewriting
Next topic
Contextual Retrieval