{"record":{"id":"1c47a4d5ca00c88e","repo":"run-llama/llama_index","slug":"must-provide-either-template-or-selector","errorCode":null,"errorMessage":"Must provide either template or selector.","messagePattern":"Must provide either template or selector\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/prompts/base.py","lineNumber":401,"sourceCode":"        selector: Optional[\"LangchainSelector\"] = None,\n        output_parser: Optional[BaseOutputParser] = None,\n        prompt_type: str = PromptType.CUSTOM,\n        metadata: Optional[Dict[str, Any]] = None,\n        template_var_mappings: Optional[Dict[str, Any]] = None,\n        function_mappings: Optional[Dict[str, Callable]] = None,\n        requires_langchain_llm: bool = False,\n    ) -> None:\n        try:\n            from llama_index.core.bridge.langchain import (\n                ConditionalPromptSelector as LangchainSelector,\n            )\n        except ImportError:\n            raise ImportError(\n                \"Must install `llama_index[langchain]` to use LangchainPromptTemplate.\"\n            )\n        if selector is None:\n            if template is None:\n                raise ValueError(\"Must provide either template or selector.\")\n            selector = LangchainSelector(default_prompt=template)\n        else:\n            if template is not None:\n                raise ValueError(\"Must provide either template or selector.\")\n            selector = selector\n\n        kwargs = selector.default_prompt.partial_variables\n        template_vars = selector.default_prompt.input_variables\n\n        if metadata is None:\n            metadata = {}\n        metadata[\"prompt_type\"] = prompt_type\n\n        super().__init__(\n            selector=selector,\n            metadata=metadata,\n            kwargs=kwargs,\n            template_vars=template_vars,","sourceCodeStart":383,"sourceCodeEnd":419,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/prompts/base.py#L383-L419","documentation":"LangchainPromptTemplate.__init__ accepts either a langchain template (which it wraps in a ConditionalPromptSelector) or a pre-built selector — but not neither. When selector is None and template is None there is nothing to format, so this ValueError is raised immediately after the langchain import check.","triggerScenarios":"Calling LangchainPromptTemplate() with no arguments, or with only output_parser/metadata while both template and selector are None.","commonSituations":"Subclassing or wrapping LangchainPromptTemplate and forgetting to forward the template; passing the template under the wrong kwarg; refactoring from PromptTemplate where template was positional.","solutions":["Pass template=<langchain PromptTemplate> (a selector is created for you).","Or pass selector=ConditionalPromptSelector(default_prompt=...) if you need conditional selection per LLM.","Check wrapper code forwards template explicitly."],"exampleFix":"# before\nlc_prompt = LangchainPromptTemplate()\n# after\nfrom langchain.prompts import PromptTemplate as LCPrompt\nlc_prompt = LangchainPromptTemplate(template=LCPrompt.from_template(\"Q: {question}\"))","handlingStrategy":"validation","validationCode":"if selector is None and template is None:\n    raise ValueError(\"LangchainPromptTemplate needs a template or a selector\")","typeGuard":"def has_template_source(template, selector) -> bool:\n    return (template is not None) or (selector is not None)","tryCatchPattern":null,"preventionTips":["In wrappers, make template a required positional argument.","Prefer passing template; use selector only for LLM-conditional prompt selection."],"tags":["validation","langchain","prompt","configuration"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}