#23273·llama_index

[Bug] Choice-select parser accepts non-positive answer numbers, silently indexing the last node

Author: yetugeCreated Sep 27, 2026Updated Sep 27, 2026

Describe the bug

default_parse_choice_select_answer_fn validates only the upper bound of a parsed choice number, so a non-positive number survives parsing and its callers index the wrong node instead of failing.

To Reproduce

python
from llama_index.core.indices.utils import default_parse_choice_select_answer_fn

# A model answering "0" for a batch of 3 nodes
nums, relevances = default_parse_choice_select_answer_fn(
    "answer_num: 0, answer_relevance: 9.5", num_choices=3
)
print(nums)  # [0]

LLMRerank, LLMRerank's structured variant, and the list / document-summary retrievers all turn each parsed number into a batch index with int(choice) - 1:

python
choice_idxs = [int(choice) - 1 for choice in raw_choices]
choice_nodes = [nodes_batch[idx] for idx in choice_idxs]

So the parsed 0 becomes -1, which Python resolves to the last node — no exception, no warning. A model that answers 0 (or a negative number) silently retrieves and returns the wrong document, while a genuinely out-of-range 4 for a batch of 3 is correctly rejected by the existing check.

Reproduced end-to-end with a SummaryIndex and a stubbed parser returning [0]:

retrieved: ['document number 3']   # index -1 == last node, chosen by a bogus 0

Expected behavior

A choice number outside 1..num_choices should be dropped, exactly as numbers above num_choices already are. The valid range is 1-based, so 0 and negative values are malformed input, not "the last node".

Version

0.14.25

Additional context

This is distinct from #23166, which changes the same function to fix a different defect (an unpaired entry left in answer_nums when a line's relevance fails to parse). #23166 does not touch the bounds check. I am happy to rebase on top of that change if it lands first.