Describe the bug
SimpleInMemoryExampleStore.get_examples(data, top_k=0, ...) is meant to return at most top_k examples, but top_k=0 returns every stored example instead of none, in both the recency-based path (no embedding model) and the cosine-similarity path (with an embedding model).
Root cause: Python's negative-slice quirk where -0 == 0, so list[-top_k:] with top_k=0 becomes list[-0:] == list[0:] == the full list, not an empty slice.
Two call sites hit this:
# recency-based path, no embedding model
if not self.embedding_model or not self._embeddings_list:
return self._examples[-top_k:] # top_k=0 -> returns ALL examples
# cosine-similarity path, inside _get_nearest_examples
if len(valid_indices) > 0:
top_indices = valid_indices[
np.argsort(similarities[valid_indices])[-top_k:] # same issue
]
(The other fallback branch, list(range(max(0, len(embeddings) - top_k), len(embeddings))), happens to compute the empty range correctly for top_k=0 β it's specifically the two [-top_k:] slices that are wrong.)
This is reachable from the public DynamicFewShotPrompt API: DynamicFewShotPrompt(..., max_similar_examples=0) is a reasonable way to opt out of few-shot examples entirely, but instead every stored example gets silently included in the formatted prompt.
Ragas version
Reproduced against current main (298b682).
Reproduction
from ragas.prompt.dynamic_few_shot import SimpleInMemoryExampleStore
store = SimpleInMemoryExampleStore()
for i in range(5):
store.add_example({"q": f"question {i}"}, {"a": f"answer {i}"})
result = store.get_examples({"q": "new question"}, top_k=0)
print(len(result)) # prints 5, expected 0
Expected behavior
top_k=0 returns an empty list in both code paths.
I have a fix + regression tests ready and will open a PR shortly.
Describe the bug
SimpleInMemoryExampleStore.get_examples(data, top_k=0, ...)is meant to return at mosttop_kexamples, buttop_k=0returns every stored example instead of none, in both the recency-based path (no embedding model) and the cosine-similarity path (with an embedding model).Root cause: Python's negative-slice quirk where
-0 == 0, solist[-top_k:]withtop_k=0becomeslist[-0:]==list[0:]== the full list, not an empty slice.Two call sites hit this:
(The other fallback branch,
list(range(max(0, len(embeddings) - top_k), len(embeddings))), happens to compute the empty range correctly fortop_k=0β it's specifically the two[-top_k:]slices that are wrong.)This is reachable from the public
DynamicFewShotPromptAPI:DynamicFewShotPrompt(..., max_similar_examples=0)is a reasonable way to opt out of few-shot examples entirely, but instead every stored example gets silently included in the formatted prompt.Ragas version
Reproduced against current
main(298b682).Reproduction
Expected behavior
top_k=0returns an empty list in both code paths.I have a fix + regression tests ready and will open a PR shortly.