Skip to content

DynamicFewShotPrompt: max_similar_examples=0 (top_k=0) returns all examples instead of noneΒ #2872

Description

@mittalpk

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions