Retrievers treat `run(top_k=0)` as unset because of `top_k or self.top_k`

Aperta
#4,273 1 commento 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@bogdankostic ci sta già lavorando.

Dal 18/11/2026.

  • #4290 di @farfriend — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
database

Direzione di ricerca

The issue lists affected retrievers and their source locations across many integration directories, including integrations/faiss/.../embedding_retriever.py and integrations/chroma/.../retriever.py. Start by reviewing the listed top_k or self.top_k occurrences and the existing None checks it cites, then check the relevant retriever tests. Done means runtime top_k=0 no longer falls back to the default and follows the agreed behavior consistently across affected retrievers.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

P3

Describe the bug
Many integration retrievers resolve the runtime top_k override with top_k or self.top_k (or self._top_k). Because 0 is falsy, run(top_k=0) silently uses the init default (usually 10) instead of returning no documents.

Haystack's InMemoryEmbeddingRetriever already does the None check correctly and returns [] for top_k=0. DocumentJoiner had the same or bug and was fixed in deepset-ai/haystack#12217. A few retrievers in this repo already use the None check (for example QdrantMultiVectorEmbeddingRetriever, WeaviateHybridRetriever, OpenSearchBM25Retriever, ElasticsearchInferenceDenseRetriever).

This is the same or pattern as #3854 (fixed in the SentenceTransformers ranker). #4007 later fixed other falsy Qdrant overrides (scale_score=False, score_threshold=0.0, …) but left top_k as or on purpose, with the note that it has "no meaningful falsy value". I think 0 is meaningful here, given InMemory and DocumentJoiner, but I wanted to flag that earlier decision and ask which behaviour you prefer: return [] like InMemory, or raise ValueError like the rankers. Either is better than silently falling back to 10.

To Reproduce
No server or model needed (FAISS and in-memory Qdrant/Chroma all show it):

from haystack import Document
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.document_stores.types import DuplicatePolicy
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
from haystack_integrations.document_stores.faiss import FAISSDocumentStore
from haystack_integrations.components.retrievers.faiss import FAISSEmbeddingRetriever
from haystack_integrations.document_stores.chroma import ChromaDocumentStore
from haystack_integrations.components.retrievers.chroma import ChromaEmbeddingRetriever
from haystack_integrations.document_stores.qdrant import QdrantDocumentStore
from haystack_integrations.components.retrievers.qdrant import QdrantEmbeddingRetriever

docs = [
    Document(id="a", content="x", embedding=[1.0, 0.0]),
    Document(id="b", content="y", embedding=[0.0, 1.0]),
]
q = [1.0, 0.0]

mem = InMemoryDocumentStore()
mem.write_documents(docs)
print("memory top_k=0:", [d.id for d in InMemoryEmbeddingRetriever(document_store=mem).run(query_embedding=q, top_k=0)["documents"]])

fs = FAISSDocumentStore(embedding_dim=2)
fs.write_documents(docs, policy=DuplicatePolicy.FAIL)
print("faiss top_k=0:", [d.id for d in FAISSEmbeddingRetriever(document_store=fs).run(query_embedding=q, top_k=0)["documents"]])

cs = ChromaDocumentStore()
cs.write_documents(docs, policy=DuplicatePolicy.FAIL)
print("chroma top_k=0:", [d.id for d in ChromaEmbeddingRetriever(document_store=cs).run(query_embedding=q, top_k=0)["documents"]])

qs = QdrantDocumentStore(location=":memory:", index="topk0", embedding_dim=2, progress_bar=False)
qs.write_documents(docs, policy=DuplicatePolicy.FAIL)
print("qdrant top_k=0:", [d.id for d in QdrantEmbeddingRetriever(document_store=qs).run(query_embedding=q, top_k=0)["documents"]])

Output:

memory top_k=0: []
faiss top_k=0: ['a', 'b']
chroma top_k=0: ['a', 'b']
qdrant top_k=0: ['a', 'b']

For FAISS/Chroma/Qdrant, top_k=0 returns both documents, same as the default top_k=10 on a two-document store.

Expected behavior
run(top_k=0) should not be treated as "use the default". Matching InMemory, it should return []. (Raising ValueError, as the rankers do, would also be consistent — happy to go with either if you have a preference.)

Root cause
On main at 8249f304723c78b2d92566b6b3ceaeeb35868edb, these retrievers (and a few rankers/readers) still use top_k or self.top_k / self._top_k. I only runtime-tested FAISS, Chroma and Qdrant, but the source pattern is the same in every line below.

Retrievers:

Integration Component File:line
alloydb AlloyDBEmbeddingRetriever, AlloyDBKeywordRetriever embedding_retriever.py:87, keyword_retriever.py:74
amazon_s3_vectors AmazonS3VectorsEmbeddingRetriever embedding_retriever.py:125
astra AstraEmbeddingRetriever retriever.py:93, :119
azure_ai_search AzureAISearchBM25Retriever, AzureAISearchEmbeddingRetriever, AzureAISearchHybridRetriever bm25_retriever.py:122, embedding_retriever.py:118, hybrid_retriever.py:127
chroma ChromaQueryTextRetriever, ChromaEmbeddingRetriever retriever.py:92, :119, :213, :242
elasticsearch ElasticsearchBM25Retriever, ElasticsearchEmbeddingRetriever, ElasticsearchSparseEmbeddingRetriever, ElasticsearchInferenceSparseRetriever, ElasticsearchInferenceHybridRetriever bm25_retriever.py:152, :177; embedding_retriever.py:156, :181; sparse_embedding_retriever.py:141, :166; inference_sparse_retriever.py:136, :162; inference_hybrid_retriever.py:153, :180
faiss FAISSEmbeddingRetriever embedding_retriever.py:131
google_drive GoogleDriveRetriever retriever.py:162, :199 (limit = top_k or self.top_k)
mariadb MariaDBEmbeddingRetriever, MariaDBKeywordRetriever embedding_retriever.py:109, keyword_retriever.py:103
microsoft_sharepoint MSSharePointRetriever retriever.py:152, :189
mongodb_atlas MongoDBAtlasEmbeddingRetriever, MongoDBAtlasFullTextRetriever embedding_retriever.py:140, :168; full_text_retriever.py:152, :202
opensearch OpenSearchEmbeddingRetriever embedding_retriever.py:260, :380
pgvector PgvectorEmbeddingRetriever, PgvectorKeywordRetriever embedding_retriever.py:173, :206; keyword_retriever.py:145, :175
pinecone PineconeEmbeddingRetriever embedding_retriever.py:150, :179
qdrant QdrantEmbeddingRetriever, QdrantSparseEmbeddingRetriever, QdrantHybridRetriever retriever.py:186, :233, :416, :468, :674, :737
valkey ValkeyEmbeddingRetriever embedding_retriever.py:156, :185
vespa VespaEmbeddingRetriever, VespaKeywordRetriever embedding_retriever.py:74, keyword_retriever.py:64
weaviate WeaviateBM25Retriever, WeaviateEmbeddingRetriever bm25_retriever.py:126, :149; embedding_retriever.py:144, :192

The same or pattern also remains in HuggingFaceAPIRanker (ranker.py:203), JinaRanker (ranker.py:121), and TransformersExtractiveReader (extractive_reader.py:599). I have not runtime-tested those three.

Possible fix
Replace top_k or self.top_k with self.top_k if top_k is None else top_k (the form already used in the retrievers that got this right). Add a run(top_k=0) regression test on a store that can run offline. Happy to open a PR, starting with the locally testable integrations (FAISS, Chroma, Qdrant) and following up on the others, once you confirm whether 0 should return [] or raise.

Describe your environment (please complete the following information):

  • OS: Debian GNU/Linux 13 (trixie), x86_64
  • Python: 3.13.5
  • Haystack version: haystack-ai 3.5.0
  • Integration version: faiss-haystack 3.0.1 (faiss-cpu 1.16.0), chroma-haystack 5.1.0 (chromadb 1.5.9), qdrant-haystack 13.2.0 (qdrant-client 1.20.0). Also confirmed the or pattern is still on main at 8249f30.
Lingua principale
Python
Stelle
212
Fork
396
Merge medio
2g 9h
PR unite (30g)
95

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di deepset-ai/haystack-core-integrations

Tutte le issue di deepset-ai/haystack-core-integrations

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.