HyDE – Hypothetical Document Embeddings

Hey 👋🏻, I am , a Software Engineer from India. I am interested in, write about, and develop (open source) software solutions for and with JavaScript, ReactJs. 📬 Get in touch
Twitter: https://x.com/SankalpHaritash Blog: https://sankalp-haritash.hashnode.dev/ LinkedIn: https://www.linkedin.com/in/sankalp-haritash/ GitHub: https://github.com/SankalpHaritash21
📧 Sign up for my newsletter: https://sankalp-haritash.hashnode.dev/newsletter
Imagine you're at a small chai tapri with your friends. You both are talking about:
"Yaar bijli ka bill itna kyun aa raha hai?
Koi solar ka jugaad hoga kya?"
Now, If we directly start searching for “solar”, then search engine try to find those exact words in websites.
But search engine never talk likes an human but we do. As an human we use half-formed questions, mix languages, use context, spelling mistakes, etc.. Then also we expect smart answers from search engine.
To fix this issue HyDe comes into play
What is HyDE?
HyDE = Hypothetical Document Embeddings.
It’s a method where:
AI imagines a full answer to your query (even before searching).
It converts that answer into numbers (vector embeddings).
Then it searches for real documents with similar meanings.
Think of it like asking a friend, "What should I cook with just potatoes and onions?"
Instead of googling it, your friend imagines a dish first ("aloo-pyaz ki sabzi"), and then checks their recipe book to see if there’s a proper match.
That's HyDE.
vector embedding is just a way to represent text as a list of numbers —
typically high-dimensional
vectors like [0.23, -0.98, ..., 0.45] with hundreds or thousands of
dimensions.
These numbers capture the meaning of the sentence — so that:
Similar ideas = similar vectors
Unrelated ideas = far apart vectors
How it works behind the scenes:

Imagine an app for farmers that answers natural questions:
User: “Barish nahi ho rahi, paani ka kya upay hai?”
🔍 HyDE:
AI imagines a detailed solution: “Rainwater harvesting using tanks and check wells”
It turns that into an vector embedding.
Searches a local knowledge database or government scheme PDFs with similar concepts.
Returns the best matches (like “Jal Shakti Abhiyan: Catch the Rain” scheme PDFs).
🧱 Regular Retrieval:
- Query → Vector → Compare with documents
✅ Works well if the query is close to the doc.
🔮 HyDE Retrieval:
- Query → Generate answer → Vectorize answer → Search
✅ Works better when query is indirect.
Implementation: HyDE
Step 1: Setup
pip install openai faiss-cpu gript langchain langchain-openai python-dotenv
Step 2: API Key Setup
OPENAI_API_KEY=your-openai-api-key
Step 3: Create LLM + LangChain QA Pipeline
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers.openai_tools import PydanticToolsParser
from langchain_core.pydantic_v1 import BaseModel, Field
# Load API key from .env
load_dotenv()
os.environ["OPENAI_API_KEY"] = "Your key"
# LangChain system prompt
system_prompt = """You are an expert about LangChain, LangGraph,
LangServe, and LangSmith. ..."""
prompt = ChatPromptTemplate.from_messages([
("system", system_prompt),
("human", "{question}")
])
llm = ChatOpenAI(model="gpt-4o", temperature=0)
# Chain for simple QA
qa_chain = prompt | llm | StrOutputParser()
response = qa_chain.invoke({
"question": "how to use multi-modal models in a chain and turn
chain into a rest api"
})
print("\n[QA Chain Response]:", response)
🧠 Step 4: Add HYDE (Hypothetical Answer Generation)
hyde_chain = RunnablePassthrough.assign(hypothetical_document=qa_chain)
hyde_output = hyde_chain.invoke({
"question": "how to use multi-modal models in a chain and turn chain
into a rest api"
})
print("\n[HYDE Chain Output]:", hyde_output)
Step 6: Add Structured Output via Pydantic
class Query(BaseModel):
answer: str = Field(..., description="Tutorial-style answer.")
llm_with_tools = llm.bind_tools([Query])
structured_chain = prompt | llm_with_tools |
PydanticToolsParser(tools=[Query])
structured_response = structured_chain.invoke({
"question": "how to use multi-modal models in a chain and turn chain
into a rest api"
})
print("\n[Structured Output]:", structured_response)
🌍 Where we can use HyDE
| Sector | Use Case Example |
| Agriculture | Smart query-answering for farmers using local schemes & techniques |
| Healthcare | Village clinics answering symptom-based queries using verified health docs |
| Education | Students asking “How to learn coding at home?” and getting tutorial resources |
| Government Portals | “How to get subsidy for water tank?” → Matches local scheme documents |
HyDE isn’t just a fancy AI term it help Ai to process data same as how humans think.
We imagine a story, then match reality.
AI is just catching up.
In the era of digital transformation, if you’re building anything that answers questions for the next billion users, HyDE can help us in improving its system architecture.
Check out My Code at: GitHub
Leave your thought in comments.





