Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

MultiRetriever rejects top_k=0 at init but accepts it at runtime

Aperta Adatta ai principianti
#12,970 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
88/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
search

Direzione di ricerca

Inizia in multi_retriever.py, nella validazione di init intorno alle righe 116-119, quindi confrontala con _resolve_top_k e con il ramo di run descritto nell’issue. Aggiungi una copertura di regressione per gli argomenti del costruttore con valore 0 e verifica che run() restituisca un risultato documents vuoto. Il lavoro è completato quando 0 viene accettato all’inizializzazione, i valori negativi continuano a generare un errore e la descrizione di ValueError riflette la nuova soglia.

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

Descrizione

Problem

MultiRetriever.__init__ rejects top_k=0 (and top_k_per_retriever=0), but the runtime validation in _resolve_top_k accepts top_k=0 with an explicit docstring saying it is valid. The component is internally inconsistent: a value that the runtime treats as "no documents returned" cannot be set at construction time.

Reproduction

On current main:

from haystack.components.retrievers import MultiRetriever

try:
    MultiRetriever(retrievers={}, top_k=0)
except ValueError as e:
    print(repr(e))
# ValueError("top_k must be greater than 0, but got 0")

try:
    MultiRetriever(retrievers={}, top_k_per_retriever=0)
except ValueError as e:
    print(repr(e))
# ValueError("top_k_per_retriever must be greater than 0, but got 0")

The init-time checks at multi_retriever.py:116-119 raise on 0 even though _resolve_top_k (multi_retriever.py:137-142) explicitly allows 0, with the docstring:

Resolve runtime values against the init defaults and reject negatives.

A resolved value of 0 is valid and means no documents are returned.

And run itself handles top_k=0 (multi_retriever.py:264):

if resolved_top_k == 0 or resolved_top_k_per_retriever == 0:

So top_k=0 is a meaningful runtime value — it returns an empty result — but the constructor refuses to accept it.

Expected behavior

MultiRetriever(top_k=0) and MultiRetriever(top_k_per_retriever=0) should construct successfully and produce empty results at runtime, matching what _resolve_top_k and run already support.

Suggested scope

Change the __init__ checks at multi_retriever.py:116-119 from <= 0 to < 0, mirroring the runtime validation. Update the :raises ValueError: description at line 114 to drop the "not greater than 0" wording (now "is negative"). No public API change, no behavior change beyond accepting 0. Add a regression test that constructs MultiRetriever(top_k=0) and verifies run() returns {"documents": []} (or the equivalent empty branch for the chosen join_mode).

Alternatives considered
  1. Tightening the runtime to reject 0. Not chosen — _resolve_top_k docstring, the run short-circuit, and the empty-slice semantics (merged[:0] == []) all treat 0 as a valid "no documents" value.
  2. Treating 0 as None. Not chosen — explicit 0 is useful for callers who want to disable a retriever's contribution without dropping it from retrievers.
Acceptance criteria
  • MultiRetriever(retrievers={}, top_k=0) constructs without error.
  • MultiRetriever(retrievers={}, top_k_per_retriever=0) constructs without error.
  • run() with a resolved top_k or top_k_per_retriever of 0 returns {"documents": []} (or the documented equivalent for join_mode="concatenate").
  • The init-time :raises ValueError: description matches the new threshold.
Backward compatibility

None. The change accepts a previously-rejected value; existing callers passing positive values see no difference.

Lingua principale
Python
Stelle
26.6k
Fork
3.2k
Merge medio
1g 12h
PR unite (30g)
237

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

Tutte le issue di deepset-ai/haystack

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.