run-llama/llama_index · error · ValueError

Prompt should have output parser.

Error message

Prompt should have output parser.

What it means

LLMSingleSelector parses the LLM's free-text choice with a prompt output parser. The constructor requires prompt.output_parser to be set, because without it the selector cannot turn the raw completion into a SelectionWithScores; passing a custom SingleSelectPrompt lacking an output_parser raises ValueError('Prompt should have output parser.') at init time.

Source

Thrown at llama-index-core/llama_index/core/selectors/llm_selectors.py:70

    LLM-based selector that chooses one out of many options.

    Args:
        LLM (LLM): An LLM.
        prompt (SingleSelectPrompt): A LLM prompt for selecting one out of many options.

    """

    def __init__(
        self,
        llm: LLM,
        prompt: SingleSelectPrompt,
    ) -> None:
        self._llm = llm
        self._prompt: BasePromptTemplate = prompt

        if self._prompt.output_parser is None:
            raise ValueError("Prompt should have output parser.")

    @classmethod
    def from_defaults(
        cls,
        llm: Optional[LLM] = None,
        prompt_template_str: Optional[str] = None,
        output_parser: Optional[BaseOutputParser] = None,
    ) -> "LLMSingleSelector":
        # optionally initialize defaults
        llm = llm or Settings.llm
        prompt_template_str = prompt_template_str or DEFAULT_SINGLE_SELECT_PROMPT_TMPL
        output_parser = output_parser or SelectionOutputParser()

        # construct prompt
        prompt = SingleSelectPrompt(
            template=prompt_template_str,
            output_parser=output_parser,
            prompt_type=PromptType.SINGLE_SELECT,

View on GitHub (pinned to afd0fef371)

Solutions

  1. Pass output_parser=SelectionOutputParser() when building the custom prompt
  2. Prefer from_defaults(prompt_template_str=...) which wires the output parser automatically
  3. Reuse the default prompt and only tweak metadata/choices fed to it

Example fix

# before
from llama_index.core.prompts import PromptTemplate
selector = LLMSingleSelector(llm=llm, prompt=SingleSelectPrompt.from_template(my_tmpl))  # no parser

# after
from llama_index.core.selectors.llm_selectors import SelectionOutputParser
selector = LLMSingleSelector.from_defaults(llm=llm, prompt_template_str=my_tmpl)  # parser auto-attached
Defensive patterns

Strategy: validation

Validate before calling

assert prompt.output_parser is not None, "SingleSelectPrompt needs SelectionOutputParser"

Type guard

def prompt_has_parser(prompt) -> bool:
    return getattr(prompt, "output_parser", None) is not None

Prevention

When it happens

Trigger: Constructing LLMSingleSelector(llm=..., prompt=SingleSelectPrompt(...)) directly with a prompt built without an output parser (the default DEFAULT_SINGLE_SELECT_PROMPT includes one; custom template strings built via PromptTemplate do not unless passed explicitly).

Common situations: Customizing the selection prompt by building a fresh SingleSelectPrompt and forgetting output_parser=SelectionOutputParser(); version upgrades where the prompt constructor signature changed.

Related errors


AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15). Data as JSON: /api/errors/e70435f3f7335cbd. Report an issue: GitHub.