{"record":{"id":"81f7d78e3358857c","repo":"run-llama/llama_index","slug":"must-provide-a-langchainllm","errorCode":null,"errorMessage":"Must provide a LangChainLLM.","messagePattern":"Must provide a LangChainLLM\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/prompts/base.py","lineNumber":456,"sourceCode":"        lc_selector = LangchainSelector(\n            default_prompt=default_prompt, conditionals=conditionals\n        )\n\n        # copy full prompt object, replace selector\n        lc_prompt = deepcopy(self)\n        lc_prompt.selector = lc_selector\n        return lc_prompt\n\n    def format(self, llm: Optional[BaseLLM] = None, **kwargs: Any) -> str:\n        \"\"\"Format the prompt into a string.\"\"\"\n        from llama_index.llms.langchain import LangChainLLM  # pants: no-infer-dep\n\n        if llm is not None:\n            # if llamaindex LLM is provided, and we require a langchain LLM,\n            # then error. but otherwise if `requires_langchain_llm` is False,\n            # then we can just use the default prompt\n            if not isinstance(llm, LangChainLLM) and self.requires_langchain_llm:\n                raise ValueError(\"Must provide a LangChainLLM.\")\n            elif not isinstance(llm, LangChainLLM):\n                lc_template = self.selector.default_prompt\n            else:\n                lc_template = self.selector.get_prompt(llm=llm.llm)\n        else:\n            lc_template = self.selector.default_prompt\n\n        # if there's mappings specified, make sure those are used\n        mapped_kwargs = self._map_all_vars(kwargs)\n        return lc_template.format(**mapped_kwargs)\n\n    def format_messages(\n        self, llm: Optional[BaseLLM] = None, **kwargs: Any\n    ) -> List[ChatMessage]:\n        \"\"\"Format the prompt into a list of chat messages.\"\"\"\n        from llama_index.llms.langchain import LangChainLLM  # pants: no-infer-dep\n        from llama_index.llms.langchain.utils import (\n            from_lc_messages,","sourceCodeStart":438,"sourceCodeEnd":474,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/prompts/base.py#L438-L474","documentation":"LangchainPromptTemplate.format() needs to resolve which langchain template to use. If the template was flagged requires_langchain_llm=True and the passed llm is not a LangChainLLM wrapper, llama-index cannot select a compatible langchain prompt, so it raises this ValueError. Without the flag, a non-langchain LLM silently falls back to the default prompt.","triggerScenarios":"Calling lc_prompt.format(llm=OpenAI(...), ...) on a LangchainPromptTemplate constructed with requires_langchain_llm=True; also when the llama-index-llms-langchain integration is missing so isinstance never matches.","commonSituations":"Mixing native llama-index LLMs with langchain prompt templates in one pipeline; setting requires_langchain_llm defensively without wrapping the LLM; forgetting to wrap via LangChainLLM(any_llm).","solutions":["Wrap your LLM: from llama_index.llms.langchain import LangChainLLM; llm = LangChainLLM(llm=your_any_llm).","Or set requires_langchain_llm=False (default) so non-langchain LLMs use the default prompt.","Ensure llama-index-llms-langchain is installed so the isinstance check can pass."],"exampleFix":"# before\nlc_prompt = LangchainPromptTemplate(template=lc_tmpl, requires_langchain_llm=True)\ntext = lc_prompt.format(llm=OpenAI())\n# after\nfrom llama_index.llms.langchain import LangChainLLM\nlc_prompt = LangchainPromptTemplate(template=lc_tmpl, requires_langchain_llm=True)\ntext = lc_prompt.format(llm=LangChainLLM(llm=some_langchain_chat_model))","handlingStrategy":"type-guard","validationCode":"from llama_index.llms.langchain import LangChainLLM\n\nif lc_prompt.requires_langchain_llm and not isinstance(llm, LangChainLLM):\n    raise ValueError(\"wrap the LLM: LangChainLLM(llm=model)\")","typeGuard":"from llama_index.llms.langchain import LangChainLLM\nfrom llama_index.core.llms import BaseLLM\n\ndef is_langchain_llm(llm: BaseLLM) -> bool:\n    return isinstance(llm, LangChainLLM)","tryCatchPattern":null,"preventionTips":["Only set requires_langchain_llm=True when the pipeline exclusively uses LangChainLLM.","Keep langchain-templated prompts in a separate pipeline from native llama-index prompts."],"tags":["langchain","llm-wrapper","prompt","runtime"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}