ValueError
haystack.document_stores.base.ValueError
Stack trace
ValueError: Document store not set for retriever. Please assign a document store before using the retriever.
Why it happens
Haystack retrievers require a document store to fetch documents for querying. If the document store attribute is not set or linked to the retriever instance, the retriever cannot function and raises this error.
Detection
Check if the retriever instance has a non-null document_store attribute before calling retrieval methods to catch this error early.
Causes & fixes
Retriever instance was created without passing a document store parameter.
Instantiate the retriever with a valid document store object, e.g., `retriever = DenseRetriever(document_store=your_document_store)`.
Document store was not assigned to the retriever after creation.
Assign the document store explicitly after retriever creation using `retriever.document_store = your_document_store`.
Using a retriever method before initializing or connecting the document store backend.
Ensure the document store is properly initialized and connected before using the retriever.
Code: broken vs fixed
from haystack.nodes import DenseRetriever
retriever = DenseRetriever() # Missing document_store parameter
retriever.retrieve(query="example") # This line triggers the error import os
from haystack.document_stores import FAISSDocumentStore
from haystack.nodes import DenseRetriever
os.environ["FAISS_INDEX_PATH"] = "/path/to/index"
document_store = FAISSDocumentStore(faiss_index_path=os.environ["FAISS_INDEX_PATH"])
retriever = DenseRetriever(document_store=document_store) # Fixed: document_store set
results = retriever.retrieve(query="example")
print(results) Workaround
Wrap retriever calls in try/except ValueError and check if the document_store attribute is None; if so, initialize or assign a document store before retrying.
Prevention
Always initialize and assign a document store to your retriever before using it, preferably during retriever instantiation to avoid runtime errors.