High severity intermediate · Fix: 2-5 min

ValueError

langchain.memory.vectorstore.base.ValueError

What this error means
LangChain's VectorStoreRetrieverMemory raises a ValueError when the underlying vector store returns no documents, causing memory retrieval to fail.

Stack trace

traceback
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.
QUICK FIX
Populate the vector store with documents before calling VectorStoreRetrieverMemory or check for empty retrieval results before loading memory.

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

1

The vector store index is empty or uninitialized, so no documents are found during retrieval.

✓ Fix

Ensure the vector store is properly initialized and populated with documents before using VectorStoreRetrieverMemory.

2

The query used for retrieval does not match any vectors in the store, resulting in zero documents returned.

✓ Fix

Verify and adjust the query parameters or embedding method to align with the stored vectors for successful retrieval.

3

Incorrect or missing embedding function causing vector store to fail indexing or retrieval properly.

✓ Fix

Configure and pass a valid embedding function compatible with the vector store to enable correct indexing and retrieval.

Code: broken vs fixed

Broken - triggers the error
python
from langchain.memory import VectorStoreRetrieverMemory

memory = VectorStoreRetrieverMemory(retriever=my_retriever)
memory.load_memory_variables({})  # Raises ValueError if retriever returns no docs
Fixed - works correctly
python
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.")
Added a check for empty retrieval results before calling load_memory_variables to prevent ValueError from empty vector store retrieval.

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.

Python 3.9+ · langchain-core >=0.1.0 · tested on 0.2.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.