TopPSampler selects documents at or below top_p, but the docs describe exceeding it
I maintainer di solito rispondono entro 1 giorno
@kai2055 ci sta già lavorando.
Dal 16/11/2026.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 44/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- python
- Ambito
- backend, machine-learning
Direzione di ricerca
Read haystack/components/samplers/top_p.py and the linked TopPSampler docs page; compare the cumulative probability condition with the documented behavior and the provided Sarajevo and Belgrade example. The issue leaves the intended behavior undecided, so first check for maintainer guidance in the issue thread. Done means the implementation and documentation agree and the example returns the set described by the confirmed behavior.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the bug
The TopPSampler documentation and its implementation describe opposite rules for which documents are kept.
The TopPSampler docs page says:
The practical goal of the TopPSampler is to return a list of documents that, in sum, have a score larger than the top_p value.
But TopPSampler.run() in haystack/components/samplers/top_p.py selects documents with:
condition = (cumulative_probs <= top_p) | close_to_top_p
The docs describe keeping documents until their cumulative probability exceeds top_p. The code keeps documents only while their cumulative probability stays at or below top_p, so the document that would push the total above top_p is dropped.
On the example from the docs page, the two rules return different documents (see To Reproduce).
Error message
No error is raised. The component runs normally and silently returns a different set of documents than the documentation describes.
Expected behavior
Following the docs, TopPSampler(top_p=0.99) on the docs page example should return Sarajevo and Belgrade (cumulative probability 0.9976). The code returns only Sarajevo (0.9842).
It's unclear to me which behaviour is intended, so either:
- the docs should describe the current rule (keep documents while their cumulative probability stays at or below
top_p), or - the code should follow the documented rule.
For reference, standard top-p sampling (Holtzman et al., 2019) keeps the smallest set whose cumulative probability reaches p, which matches the docs rather than the code.
Additional context
The example in the TopPSampler docstring uses top_p=0.95 and expects only Sarajevo. Under the current rule, no document is selected at all, because Sarajevo alone already has a cumulative probability of 0.9842, above 0.95. Sarajevo is returned only through the fallback, which logs a warning (see output below). So the docstring example passes through the fallback path rather than the main rule.
Related: #11595 (other TopPSampler bugs in the same run() method, fixed in #11596).
I'm happy to open a PR for either fix once the intended behaviour is confirmed.
To Reproduce
import logging
from haystack import Document
from haystack.components.samplers import TopPSampler
logging.basicConfig(level=logging.WARNING)
def docs_example():
return [
Document(content="Berlin", meta={"similarity_score": -10.6}),
Document(content="Belgrade", meta={"similarity_score": -8.9}),
Document(content="Sarajevo", meta={"similarity_score": -4.6}),
]
for top_p in [0.99, 0.95]:
sampler = TopPSampler(top_p=top_p, score_field="similarity_score")
kept = sampler.run(documents=docs_example())["documents"]
print(f"top_p={top_p}: kept {[d.content for d in kept]}")
Output:
top_p=0.99: kept ['Sarajevo']
WARNING:haystack.components.samplers.top_p:Top-p sampling with p=0.95 resulted in no documents being selected. Returning the document with the highest score.
top_p=0.95: kept ['Sarajevo']
Softmax probabilities for this example, sorted by score:
| Document | Probability | Cumulative |
|---|---|---|
| Sarajevo | 0.9842 | 0.9842 |
| Belgrade | 0.0134 | 0.9976 |
| Berlin | 0.0024 | 1.0000 |
FAQ Check
- Have you had a look at our new FAQ page?
System:
- OS: Windows 11
- Haystack version: haystack-ai 3.5.0 (latest on PyPI;
top_p.pyonmainunchanged since #12651) - Python: 3.12.10
- torch: 2.15.0+cpu
- Lingua principale
- Python
- Stelle
- 27.1k
- Fork
- 3.5k
- Merge medio
- 1g 8h
- PR unite (30g)
- 239
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di deepset-ai/haystack
-
BlockingConfirmationStrategy ignores an empty argument replacementForse già presa @Aarkin7 l’ha presa 3 giorni fa. Aperta
Difficoltà 1/5 1-3 ore Idoneità per principianti 82/100
deepset-ai/haystack#13863 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
P2
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
deepset-ai/haystack#13830 · 4 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
DocumentSplitter misplaces repeated literal chunks from an overlapping custom splitterForse già presa @davidsbatista l’ha presa 7 giorni fa. ApertaP2
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
deepset-ai/haystack#13806 · 2 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
CSVToDocument accepts an unsupported conversion_mode and silently converts in row modeForse già presa @Lesereingrape l’ha presa 54 giorni fa. ApertaP3
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
deepset-ai/haystack#13070 ·
I maintainer di solito rispondono entro 1 giorno
-
InMemoryDocumentStore: count_unique_metadata_by_filter and get_metadata_field_unique_values disagree on int/float/bool valuesForse già presa @sjrl l’ha presa 14 giorni fa. ApertaP3
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
deepset-ai/haystack#13040 · 1 commento · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di deepset-ai/haystack
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
micronaut-projects/micronaut-core#14585 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
langgenius/graphon#443 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno
-
[BUG]: Management endpoints /v1/memory/stats and /v1/memory/counts bypass API key authenticationAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
docling-project/docling-serve#788 ·
I maintainer di solito rispondono entro 1 giorno
-
Mend: dependency security vulnerability untriaged
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
opensearch-project/cross-cluster-replication#1805 ·
I maintainer di solito rispondono entro 1 giorno