{"record":{"id":"8ee79176cd9e5a43","repo":"run-llama/llama_index","slug":"must-provide-either-prompt-or-prompt-template-str-8ee791","errorCode":null,"errorMessage":"Must provide either prompt or prompt_template_str.","messagePattern":"Must provide either prompt or prompt_template_str\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/program/llm_program.py","lineNumber":63,"sourceCode":"        self._prompt = prompt\n        self._verbose = verbose\n\n        self._prompt.output_parser = output_parser\n\n    @classmethod\n    def from_defaults(\n        cls,\n        output_parser: Optional[BaseOutputParser] = None,\n        output_cls: Optional[Type[Model]] = None,\n        prompt_template_str: Optional[str] = None,\n        prompt: Optional[BasePromptTemplate] = None,\n        llm: Optional[LLM] = None,\n        verbose: bool = False,\n        **kwargs: Any,\n    ) -> \"LLMTextCompletionProgram[Model]\":\n        llm = llm or Settings.llm\n        if prompt is None and prompt_template_str is None:\n            raise ValueError(\"Must provide either prompt or prompt_template_str.\")\n        if prompt is not None and prompt_template_str is not None:\n            raise ValueError(\"Must provide either prompt or prompt_template_str.\")\n        if prompt_template_str is not None:\n            prompt = PromptTemplate(prompt_template_str)\n\n        # decide default output class if not set\n        if output_cls is None:\n            if not isinstance(output_parser, PydanticOutputParser):\n                raise ValueError(\"Output parser must be PydanticOutputParser.\")\n            output_cls = output_parser.output_cls\n        else:\n            if output_parser is None:\n                output_parser = PydanticOutputParser(output_cls=output_cls)\n\n        return cls(\n            output_parser,\n            output_cls,\n            prompt=cast(PromptTemplate, prompt),","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/program/llm_program.py#L45-L81","documentation":"LLMTextCompletionProgram.from_defaults requires exactly one prompt source: either a formatted prompt_template_str string or a pre-built BasePromptTemplate. This error fires when both arguments are None, leaving the program with no prompt to format. It is raised before any LLM call, so it is purely a caller configuration mistake.","triggerScenarios":"Calling LLMTextCompletionProgram.from_defaults(output_parser=..., output_cls=...) with neither prompt= nor prompt_template_str=. Also happens when the prompt variable is accidentally passed under a different kwarg name or is None due to earlier logic.","commonSituations":"Copy-pasting an example that used prompt_template_str but renaming the argument; passing prompt=None because a conditional upstream never assigned it; migrating from an older API where output_cls alone was enough.","solutions":["Pass exactly one of prompt_template_str='...' or prompt=PromptTemplate(...) to from_defaults.","If building the prompt dynamically, assert it is not None before calling from_defaults.","Do not pass both arguments — a sibling check at line 65 rejects that combination too."],"exampleFix":"// before\nprogram = LLMTextCompletionProgram.from_defaults(\n    output_cls=Album,\n    verbose=True,\n)\n// after\nprogram = LLMTextCompletionProgram.from_defaults(\n    output_parser=PydanticOutputParser(output_cls=Album),\n    prompt_template_str=\"Generate an example album: {album_name}\",\n    verbose=True,\n)","handlingStrategy":"validation","validationCode":"def build_program(output_cls, prompt=None, prompt_template_str=None, **kw):\n    if prompt is None and prompt_template_str is None:\n        raise ValueError(\"program requires prompt or prompt_template_str\")\n    return LLMTextCompletionProgram.from_defaults(\n        output_parser=PydanticOutputParser(output_cls=output_cls),\n        prompt=prompt,\n        prompt_template_str=prompt_template_str,\n        **kw,\n    )","typeGuard":"from typing import Optional\nfrom llama_index.core.prompts import BasePromptTemplate\n\ndef has_prompt_source(prompt: Optional[BasePromptTemplate],\n                      template_str: Optional[str]) -> bool:\n    return (prompt is None) != (template_str is None)","tryCatchPattern":null,"preventionTips":["Centralize program construction in one helper that validates prompt arguments.","Exactly-one-of arguments are best enforced with a small XOR-style check before calling from_defaults."],"tags":["validation","prompt","configuration","program"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}