{"record":{"id":"e70435f3f7335cbd","repo":"run-llama/llama_index","slug":"prompt-should-have-output-parser-e70435","errorCode":null,"errorMessage":"Prompt should have output parser.","messagePattern":"Prompt should have output parser\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/selectors/llm_selectors.py","lineNumber":70,"sourceCode":"\n    LLM-based selector that chooses one out of many options.\n\n    Args:\n        LLM (LLM): An LLM.\n        prompt (SingleSelectPrompt): A LLM prompt for selecting one out of many options.\n\n    \"\"\"\n\n    def __init__(\n        self,\n        llm: LLM,\n        prompt: SingleSelectPrompt,\n    ) -> None:\n        self._llm = llm\n        self._prompt: BasePromptTemplate = prompt\n\n        if self._prompt.output_parser is None:\n            raise ValueError(\"Prompt should have output parser.\")\n\n    @classmethod\n    def from_defaults(\n        cls,\n        llm: Optional[LLM] = None,\n        prompt_template_str: Optional[str] = None,\n        output_parser: Optional[BaseOutputParser] = None,\n    ) -> \"LLMSingleSelector\":\n        # optionally initialize defaults\n        llm = llm or Settings.llm\n        prompt_template_str = prompt_template_str or DEFAULT_SINGLE_SELECT_PROMPT_TMPL\n        output_parser = output_parser or SelectionOutputParser()\n\n        # construct prompt\n        prompt = SingleSelectPrompt(\n            template=prompt_template_str,\n            output_parser=output_parser,\n            prompt_type=PromptType.SINGLE_SELECT,","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/selectors/llm_selectors.py#L52-L88","documentation":"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.","triggerScenarios":"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).","commonSituations":"Customizing the selection prompt by building a fresh SingleSelectPrompt and forgetting output_parser=SelectionOutputParser(); version upgrades where the prompt constructor signature changed.","solutions":["Pass output_parser=SelectionOutputParser() when building the custom prompt","Prefer from_defaults(prompt_template_str=...) which wires the output parser automatically","Reuse the default prompt and only tweak metadata/choices fed to it"],"exampleFix":"# before\nfrom llama_index.core.prompts import PromptTemplate\nselector = LLMSingleSelector(llm=llm, prompt=SingleSelectPrompt.from_template(my_tmpl))  # no parser\n\n# after\nfrom llama_index.core.selectors.llm_selectors import SelectionOutputParser\nselector = LLMSingleSelector.from_defaults(llm=llm, prompt_template_str=my_tmpl)  # parser auto-attached","handlingStrategy":"validation","validationCode":"assert prompt.output_parser is not None, \"SingleSelectPrompt needs SelectionOutputParser\"","typeGuard":"def prompt_has_parser(prompt) -> bool:\n    return getattr(prompt, \"output_parser\", None) is not None","tryCatchPattern":null,"preventionTips":["Use from_defaults() instead of direct construction","Always attach SelectionOutputParser to custom select prompts","Cover selector construction in smoke tests"],"tags":["selector","llm","prompts","config"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}