Skip to content

InMemoryExampleStore.get_nearest_examples returns all examples when top_k=0 #2877

Description

@bharadwaj-pendyala

Passing top_k=0 to InMemoryExampleStore.get_nearest_examples() returns every example above the threshold instead of returning nothing.

The culprit is the negative slice in src/ragas/prompt/few_shot_pydantic_prompt.py:

top_indices = valid_indices[np.argsort(similarities[valid_indices])[-top_k:]]

With top_k=0, [-top_k:] becomes [-0:], and numpy reads that as [0:], the whole array. So asking for zero examples quietly hands you all of them.

This is the same bug that was already fixed for SimpleInMemoryExampleStore in #2872. That fix lives in a different file and doesn't touch InMemoryExampleStore, so this store still has it.

Quick repro:

from ragas.prompt.few_shot_pydantic_prompt import InMemoryExampleStore

query = [1.0, 0.0]
embeddings = [[1.0, 0.0], [0.9, 0.1], [0.8, 0.2]]

InMemoryExampleStore.get_nearest_examples(query, embeddings, top_k=0, threshold=0.0)
# returns [2, 1, 0], expected []

get_examples(..., top_k=0) hits the same path since it just delegates here.

Expected: any non-positive top_k should return an empty list.

Version: main (0.4.3).

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