deepset-ai/haystack · error · TypeError

document_store must be an instance of InMemoryDocumentStore

Error message

document_store must be an instance of InMemoryDocumentStore

What it means

BM25Retriever (the in-memory variant) only supports InMemoryDocumentStore because BM25 scoring is implemented there. Passing any other DocumentStore raises a TypeError in __init__, not a ValueError.

Source

Thrown at haystack/components/retrievers/in_memory/bm25_retriever.py:71

            An instance of InMemoryDocumentStore where the retriever should search for relevant documents.
        :param filters:
            A dictionary with filters to narrow down the retriever's search space in the document store.
        :param top_k:
            The maximum number of documents to retrieve.
        :param scale_score:
            When `True`, scales the score of retrieved documents to a range of 0 to 1, where 1 means extremely relevant.
            When `False`, uses raw similarity scores.
        :param filter_policy: The filter policy to apply during retrieval.
        Filter policy determines how filters are applied when retrieving documents. You can choose:
        - `REPLACE` (default): Overrides the initialization filters with the filters specified at runtime.
        Use this policy to dynamically change filtering for specific queries.
        - `MERGE`: Combines runtime filters with initialization filters to narrow down the search.
        :raises TypeError: If the document_store is not an instance of InMemoryDocumentStore.
        :raises ValueError:
            If the specified `top_k` is not > 0.
        """
        if not isinstance(document_store, InMemoryDocumentStore):
            raise TypeError("document_store must be an instance of InMemoryDocumentStore")

        self.document_store = document_store

        if top_k <= 0:
            raise ValueError(f"top_k must be greater than 0. Currently, the top_k is {top_k}")

        self.filters = filters
        self.top_k = top_k
        self.scale_score = scale_score
        self.filter_policy = filter_policy

    def _get_telemetry_data(self) -> dict[str, Any]:
        """
        Data that is sent to Posthog for usage analytics.
        """
        return {"document_store": type(self.document_store).__name__}

    def to_dict(self) -> dict[str, Any]:

View on GitHub (pinned to e318778c9b)

Solutions

  1. Use the retriever class matching your store, e.g. QdrantEmbeddingRetriever / store-specific BM25 retriever
  2. Switch document_store to InMemoryDocumentStore if BM25 in-memory retrieval is intended
  3. Use InMemoryBM25Retriever explicitly to make intent clear

Example fix

// before
retriever = BM25Retriever(document_store=QdrantDocumentStore(url=...))
// after
retriever = QdrantEmbeddingRetriever(document_store=qdrant_store)
# or
retriever = InMemoryBM25Retriever(document_store=InMemoryDocumentStore())
Defensive patterns

Strategy: type-guard

Validate before calling

from haystack.document_stores.in_memory import InMemoryDocumentStore
if not isinstance(store, InMemoryDocumentStore):
    raise TypeError(f"BM25Retriever needs InMemoryDocumentStore, got {type(store).__name__}")

Type guard

def supports_inmemory_bm25(store: object) -> bool:
    from haystack.document_stores.in_memory import InMemoryDocumentStore
    return isinstance(store, InMemoryDocumentStore)

Try / catch

try:
    retriever = InMemoryBM25Retriever(document_store=store)
except TypeError as e:
    logger.error("Incompatible document store: %s", e)
    retriever = InMemoryBM25Retriever(document_store=InMemoryDocumentStore())

Prevention

When it happens

Trigger: BM25Retriever(document_store=QdrantDocumentStore(...)) or any non-InMemoryDocumentStore instance.

Common situations: Swapping a document store in a pipeline template without switching the retriever class, or migrating from in-memory to a vector database but keeping BM25Retriever.

Related errors


AI-assisted analysis of deepset-ai/haystack@e318778c9b (2026-08-30). Data as JSON: /api/errors/148af563bccc07ef. Report an issue: GitHub.