In-memory dictionary-backed store with optional vector search.
Basic key-value storage:
from langgraph.store.memory import InMemoryStore
store = InMemoryStore()
store.put(("users", "123"), "prefs", {"theme": "dark"})
item = store.get(("users", "123"), "prefs")
Vector search using LangChain embeddings:
from langchain.embeddings import init_embeddings
from langgraph.store.memory import InMemoryStore
store = InMemoryStore(
index={
"dims": 1536,
"embed": init_embeddings("openai:text-embedding-3-small")
}
)
# Store documents
store.put(("docs",), "doc1", {"text": "Python tutorial"})
store.put(("docs",), "doc2", {"text": "TypeScript guide"})
# Search by similarity
results = store.search(("docs",), query="python programming")
Vector search using OpenAI SDK directly:
from openai import OpenAI
from langgraph.store.memory import InMemoryStore
client = OpenAI()
def embed_texts(texts: list[str]) -> list[list[float]]:
response = client.embeddings.create(
model="text-embedding-3-small",
input=texts
)
return [e.embedding for e in response.data]
store = InMemoryStore(
index={
"dims": 1536,
"embed": embed_texts
}
)
# Store documents
store.put(("docs",), "doc1", {"text": "Python tutorial"})
store.put(("docs",), "doc2", {"text": "TypeScript guide"})
# Search by similarity
results = store.search(("docs",), query="python programming")
Async vector search using OpenAI SDK:
from openai import AsyncOpenAI
from langgraph.store.memory import InMemoryStore
client = AsyncOpenAI()
async def aembed_texts(texts: list[str]) -> list[list[float]]:
response = await client.embeddings.create(
model="text-embedding-3-small",
input=texts
)
return [e.embedding for e in response.data]
store = InMemoryStore(
index={
"dims": 1536,
"embed": aembed_texts
}
)
# Store documents
await store.aput(("docs",), "doc1", {"text": "Python tutorial"})
await store.aput(("docs",), "doc2", {"text": "TypeScript guide"})
# Search by similarity
results = await store.asearch(("docs",), query="python programming")Warning:
This store keeps all data in memory. Data is lost when the process exits. For persistence, use a database-backed store like PostgresStore.
Tip:
For vector search, install numpy for better performance:
pip install numpyEnsure that an embedding function conforms to LangChain's Embeddings interface.
This function wraps arbitrary embedding functions to make them compatible with LangChain's Embeddings interface. It handles both synchronous and asynchronous functions.
Extract text from an object using a path expression or pre-tokenized path.
Tokenize a path into components.
Abstract base class for persistent key-value stores.
Stores enable persistence and memory that can be shared across threads,
scoped to user IDs, assistant IDs, or other arbitrary namespaces.
Some implementations may support semantic search capabilities through
an optional index configuration.
Configuration for indexing documents for semantic search in the store.
If not provided to the store, the store will not support vector search.
In that case, all index arguments to put() and aput() operations will be ignored.
Represents a stored item with metadata.
Operation to store, update, or delete an item in the store.
This class represents a single operation to modify the store's contents, whether adding new items, updating existing ones, or removing them.
Represents an item returned from a search operation with additional metadata.
In-memory dictionary-backed store with optional vector search.
Basic key-value storage: store = InMemoryStore() store.put(("users", "123"), "prefs", {"theme": "dark"}) item = store.get(("users", "123"), "prefs")
Vector search with embeddings: from langchain.embeddings import init_embeddings store = InMemoryStore(index={ "dims": 1536, "embed": init_embeddings("openai:text-embedding-3-small"), "fields": ["text"], })
# Store documents
store.put(("docs",), "doc1", {"text": "Python tutorial"})
store.put(("docs",), "doc2", {"text": "TypeScript guide"})
# Search by similarity
results = store.search(("docs",), query="python programming")
Operation to retrieve a specific item by its namespace and key.
This operation allows precise retrieval of stored items using their full path (namespace) and unique identifier (key) combination.
Basic item retrieval:
GetOp(namespace=("users", "profiles"), key="user123")
GetOp(namespace=("cache", "embeddings"), key="doc456")Operation to list and filter namespaces in the store.
This operation allows exploring the organization of data, finding specific collections, and navigating the namespace hierarchy.
List all namespaces under the "documents" path:
ListNamespacesOp(
match_conditions=(MatchCondition(match_type="prefix", path=("documents",)),),
max_depth=2
)
List all namespaces that end with "v1":
ListNamespacesOp(
match_conditions=(MatchCondition(match_type="suffix", path=("v1",)),),
limit=50
)Represents a pattern for matching namespaces in the store.
This class combines a match type (prefix or suffix) with a namespace path pattern that can include wildcards to flexibly match different namespace hierarchies.
Prefix matching:
MatchCondition(match_type="prefix", path=("users", "profiles"))
Suffix matching with wildcard:
MatchCondition(match_type="suffix", path=("cache", "*"))
Simple suffix matching:
MatchCondition(match_type="suffix", path=("v1",))Operation to search for items within a specified namespace hierarchy.
This operation supports both structured filtering and natural language search within a given namespace prefix. It provides pagination through limit and offset parameters.
Natural language search support depends on your store implementation.
Search with filters and pagination:
SearchOp(
namespace_prefix=("documents",),
filter={"type": "report", "status": "active"},
limit=5,
offset=10
)
Natural language search:
SearchOp(
namespace_prefix=("users", "content"),
query="technical documentation about APIs",
limit=20
)