ValueError
langchain.memory.vectorstore.base.ValueError
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
memory.load_memory_variables({})
File "/usr/local/lib/python3.9/site-packages/langchain/memory/vectorstore/base.py", line 78, in load_memory_variables
raise ValueError("No documents found in VectorStoreRetrieverMemory.")
ValueError: No documents found in VectorStoreRetrieverMemory. Why it happens
VectorStoreRetrieverMemory depends on retrieving documents from a vector store to populate memory variables. If the vector store query returns an empty list, the memory has no data to load, triggering this ValueError. This often occurs when the vector store is uninitialized, empty, or the query parameters do not match any stored vectors.
Detection
Monitor the output of the vector store retriever query before passing it to VectorStoreRetrieverMemory; log or assert that the returned documents list is not empty to catch this error early.
Causes & fixes
The vector store index is empty or uninitialized, so no documents are found during retrieval.
Ensure the vector store is properly initialized and populated with documents before using VectorStoreRetrieverMemory.
The query used for retrieval does not match any vectors in the store, resulting in zero documents returned.
Verify and adjust the query parameters or embedding method to align with the stored vectors for successful retrieval.
Incorrect or missing embedding function causing vector store to fail indexing or retrieval properly.
Configure and pass a valid embedding function compatible with the vector store to enable correct indexing and retrieval.
Code: broken vs fixed
from langchain.memory import VectorStoreRetrieverMemory
memory = VectorStoreRetrieverMemory(retriever=my_retriever)
memory.load_memory_variables({}) # Raises ValueError if retriever returns no docs import os
from langchain.memory import VectorStoreRetrieverMemory
# Ensure environment variable for API key is set
os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')
# Assume my_retriever is properly initialized and populated
memory = VectorStoreRetrieverMemory(retriever=my_retriever)
retrieved_docs = my_retriever.get_relevant_documents("some query")
if not retrieved_docs:
print("Warning: No documents found in vector store retrieval.")
else:
memory.load_memory_variables({}) # Safe to call now
print("Memory loaded successfully.") Workaround
Wrap the call to load_memory_variables in try/except ValueError, and if caught, fallback to a default empty memory or log and skip memory loading.
Prevention
Always initialize and populate your vector store with documents before using VectorStoreRetrieverMemory, and validate retrieval results to avoid empty memory loads.