run-llama/llama_index · error · ValueError

Must provide a LangChainLLM.

Error message

Must provide a LangChainLLM.

What it means

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.

Source

Thrown at llama-index-core/llama_index/core/prompts/base.py:456

        lc_selector = LangchainSelector(
            default_prompt=default_prompt, conditionals=conditionals
        )

        # copy full prompt object, replace selector
        lc_prompt = deepcopy(self)
        lc_prompt.selector = lc_selector
        return lc_prompt

    def format(self, llm: Optional[BaseLLM] = None, **kwargs: Any) -> str:
        """Format the prompt into a string."""
        from llama_index.llms.langchain import LangChainLLM  # pants: no-infer-dep

        if llm is not None:
            # if llamaindex LLM is provided, and we require a langchain LLM,
            # then error. but otherwise if `requires_langchain_llm` is False,
            # then we can just use the default prompt
            if not isinstance(llm, LangChainLLM) and self.requires_langchain_llm:
                raise ValueError("Must provide a LangChainLLM.")
            elif not isinstance(llm, LangChainLLM):
                lc_template = self.selector.default_prompt
            else:
                lc_template = self.selector.get_prompt(llm=llm.llm)
        else:
            lc_template = self.selector.default_prompt

        # if there's mappings specified, make sure those are used
        mapped_kwargs = self._map_all_vars(kwargs)
        return lc_template.format(**mapped_kwargs)

    def format_messages(
        self, llm: Optional[BaseLLM] = None, **kwargs: Any
    ) -> List[ChatMessage]:
        """Format the prompt into a list of chat messages."""
        from llama_index.llms.langchain import LangChainLLM  # pants: no-infer-dep
        from llama_index.llms.langchain.utils import (
            from_lc_messages,

View on GitHub (pinned to afd0fef371)

Solutions

  1. Wrap your LLM: from llama_index.llms.langchain import LangChainLLM; llm = LangChainLLM(llm=your_any_llm).
  2. Or set requires_langchain_llm=False (default) so non-langchain LLMs use the default prompt.
  3. Ensure llama-index-llms-langchain is installed so the isinstance check can pass.

Example fix

# before
lc_prompt = LangchainPromptTemplate(template=lc_tmpl, requires_langchain_llm=True)
text = lc_prompt.format(llm=OpenAI())
# after
from llama_index.llms.langchain import LangChainLLM
lc_prompt = LangchainPromptTemplate(template=lc_tmpl, requires_langchain_llm=True)
text = lc_prompt.format(llm=LangChainLLM(llm=some_langchain_chat_model))
Defensive patterns

Strategy: type-guard

Validate before calling

from llama_index.llms.langchain import LangChainLLM

if lc_prompt.requires_langchain_llm and not isinstance(llm, LangChainLLM):
    raise ValueError("wrap the LLM: LangChainLLM(llm=model)")

Type guard

from llama_index.llms.langchain import LangChainLLM
from llama_index.core.llms import BaseLLM

def is_langchain_llm(llm: BaseLLM) -> bool:
    return isinstance(llm, LangChainLLM)

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15). Data as JSON: /api/errors/81f7d78e3358857c. Report an issue: GitHub.