1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
46 small wins to finish your pathNext lesson →
Semantic search in long-term memory
Semantic search in the store finds saved items by meaning, not exact words, by giving the store an embedding index and searching it with a query.
Last updated: 27 Sep, 2026 · LangChain 1.4
The store lesson looked items up by key. When you do not know the key, you want the items closest in meaning to a question. That needs an embedding index.
Giving the store an embedding index
Pass index with an embed function and its dims. The store embeds each saved value so it can compare by meaning.
def embed(texts):
# a stand-in: [mentions refund, mentions ship]
return [[float("refund" in t.lower()), float("ship" in t.lower())] for t in texts]
store = InMemoryStore(index={"embed": embed, "dims": 2})Searching by a query
Save some items, then call search with a query. The store returns the closest ones, not an exact-key match.
store.put(("faqs",), "a", {"text": "Refunds take 5 working days."})
hits = store.search(("faqs",), query="how long for a refund", limit=1)Finding a refund answer by meaning
The whole snippet, printing the closest saved item.
from langgraph.store.memory import InMemoryStore
def embed(texts):
# a tiny stand-in embedding: [mentions refund, mentions ship]
return [[float("refund" in t.lower()), float("ship" in t.lower())] for t in texts]
store = InMemoryStore(index={"embed": embed, "dims": 2})
store.put(("faqs",), "a", {"text": "Refunds take 5 working days."})
store.put(("faqs",), "b", {"text": "Orders ship within 2 days."})
hits = store.search(("faqs",), query="how long for a refund", limit=1)
print(hits[0].value["text"])How the query matched
- The query mentioned a refund, so its embedding was close to the refund item.
searchranked the items and returned the nearest one, not an exact key.- A real embedding model replaces the stand-in; the call shape is the same.
Key lookup vs semantic search
| get by key | search by query | |
|---|---|---|
| You need | The exact key | Only a question |
| Matches | One item | The closest items by meaning |
| Setup | None | An embedding index |
When to search by meaning
- Recalling the memories relevant to what a user asked.
- Finding a saved fact when you do not know its key.
- Ranking many notes by how close they are to a question.
Watch out. Search quality is only as good as the embedding. A weak or mismatched embedding model returns the wrong items, and no amount of ranking fixes that.
Related
- Previous: Memory that outlives the conversation
- Next: State beyond messages
- Reference: LangChain docs
Try it yourself
- Add a third item and a query that should match it.
- Raise
limitto 2 and read both hits in order. - Swap the stand-in embed for a real embedding model.
You understood something today that you didn't yesterday.