{"record":{"id":"c1a00fd1e049af91","repo":"run-llama/llama_index","slug":"prompt-should-have-output-parser","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/question_gen/llm_generators.py","lineNumber":30,"sourceCode":")\nfrom llama_index.core.question_gen.types import BaseQuestionGenerator, SubQuestion\nfrom llama_index.core.schema import QueryBundle\nfrom llama_index.core.settings import Settings\nfrom llama_index.core.tools.types import ToolMetadata\nfrom llama_index.core.types import BaseOutputParser\n\n\nclass LLMQuestionGenerator(BaseQuestionGenerator):\n    def __init__(\n        self,\n        llm: LLM,\n        prompt: BasePromptTemplate,\n    ) -> None:\n        self._llm = llm\n        self._prompt = 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    ) -> \"LLMQuestionGenerator\":\n        # optionally initialize defaults\n        llm = llm or Settings.llm\n        prompt_template_str = prompt_template_str or DEFAULT_SUB_QUESTION_PROMPT_TMPL\n        output_parser = output_parser or SubQuestionOutputParser()\n\n        # construct prompt\n        prompt = PromptTemplate(\n            template=prompt_template_str,\n            output_parser=output_parser,\n            prompt_type=PromptType.SUB_QUESTION,","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/question_gen/llm_generators.py#L12-L48","documentation":"LLMQuestionGenerator parses the LLM's sub-question output with prompt.output_parser. The constructor requires the prompt template to carry an output parser (a SubQuestionOutputParser) so parsed text can become SubQuestion objects; without one it raises ValueError('Prompt should have output parser.')","triggerScenarios":"Instantiating LLMQuestionGenerator(llm=..., prompt=PromptTemplate(...)) with a plain PromptTemplate that has no output_parser, or from_defaults(prompt_template_str=custom_str, output_parser=None) is fine (it defaults) but a custom prompt object lacking a parser is not.","commonSituations":"Swapping in a custom sub-question prompt by constructing BasePromptTemplate/PromptTemplate directly without attaching SubQuestionOutputParser.","solutions":["Attach the parser when building a custom prompt: PromptTemplate(template, output_parser=SubQuestionOutputParser())","Or prefer from_defaults(prompt_template_str=...) which wires SubQuestionOutputParser automatically","Keep the {output_cls} / JSON structure tokens in a custom template so the parser still works"],"exampleFix":"// before\nfrom llama_index.core import PromptTemplate\nprompt = PromptTemplate(custom_sub_question_template)  # no parser\ngen = LLMQuestionGenerator(llm=llm, prompt=prompt)\n\n// after\nfrom llama_index.core.question_gen.output_parser import SubQuestionOutputParser\nprompt = PromptTemplate(custom_sub_question_template, output_parser=SubQuestionOutputParser())\ngen = LLMQuestionGenerator(llm=llm, prompt=prompt)","handlingStrategy":"validation","validationCode":"from llama_index.core.question_gen.output_parser import SubQuestionOutputParser\n\ndef make_question_prompt(template_str: str):\n    prompt = PromptTemplate(template_str)\n    if prompt.output_parser is None:\n        prompt = PromptTemplate(template_str, output_parser=SubQuestionOutputParser())\n    return prompt","typeGuard":"def prompt_has_output_parser(prompt) -> bool:\n    \"\"\"LLMQuestionGenerator requires a parsed output.\"\"\"\n    return prompt.output_parser is not None","tryCatchPattern":null,"preventionTips":["Prefer LLMQuestionGenerator.from_defaults(prompt_template_str=...) which attaches the parser","When using a custom PromptTemplate object, always pass output_parser=SubQuestionOutputParser()","Keep the JSON/{output_cls} structure in custom templates so the parser has something to parse"],"tags":["prompt","output-parser","sub-question","constructor"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}