run-llama/llama_index · error · ImportError

Must install `llama_index[langchain]` to use LangchainPrompt

Error message

Must install `llama_index[langchain]` to use LangchainPromptTemplate.

What it means

LangchainPromptTemplate depends on langchain classes re-exported through llama_index.core.bridge.langchain. If langchain (or a compatible version) is not installed, that bridge import fails and this ImportError is raised at construction time. The template cannot function without the underlying langchain prompt machinery.

Source

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

    requires_langchain_llm: bool = False

    def __init__(
        self,
        template: Optional["LangchainTemplate"] = None,
        selector: Optional["LangchainSelector"] = None,
        output_parser: Optional[BaseOutputParser] = None,
        prompt_type: str = PromptType.CUSTOM,
        metadata: Optional[Dict[str, Any]] = None,
        template_var_mappings: Optional[Dict[str, Any]] = None,
        function_mappings: Optional[Dict[str, Callable]] = None,
        requires_langchain_llm: bool = False,
    ) -> None:
        try:
            from llama_index.core.bridge.langchain import (
                ConditionalPromptSelector as LangchainSelector,
            )
        except ImportError:
            raise ImportError(
                "Must install `llama_index[langchain]` to use LangchainPromptTemplate."
            )
        if selector is None:
            if template is None:
                raise ValueError("Must provide either template or selector.")
            selector = LangchainSelector(default_prompt=template)
        else:
            if template is not None:
                raise ValueError("Must provide either template or selector.")
            selector = selector

        kwargs = selector.default_prompt.partial_variables
        template_vars = selector.default_prompt.input_variables

        if metadata is None:
            metadata = {}
        metadata["prompt_type"] = prompt_type

View on GitHub (pinned to afd0fef371)

Solutions

  1. Install the extra: pip install 'llama-index[langchain]' (or pip install langchain).
  2. Pin compatible langchain versions if a recent upgrade broke the bridge.
  3. If you do not need langchain templates, use llama_index.core.prompts.PromptTemplate instead.

Example fix

# before
from llama_index.core.prompts import LangchainPromptTemplate
lc_prompt = LangchainPromptTemplate(template=SomeLangchainTemplate)
# after
pip install 'llama-index[langchain]'
lc_prompt = LangchainPromptTemplate(template=SomeLangchainTemplate)
Defensive patterns

Strategy: try-catch

Validate before calling

import importlib.util
if importlib.util.find_spec("llama_index.core.bridge.langchain") is None or \
   importlib.util.find_spec("langchain") is None:
    raise RuntimeError("pip install 'llama-index[langchain]'")

Type guard

def langchain_available() -> bool:
    try:
        from llama_index.core.bridge.langchain import ConditionalPromptSelector  # noqa
        return True
    except ImportError:
        return False

Try / catch

try:
    lc_prompt = LangchainPromptTemplate(template=tmpl)
except ImportError as e:
    if "langchain" in str(e):
        from llama_index.core.prompts import PromptTemplate
        lc_prompt = PromptTemplate(tmpl.template)  # plain fallback
    else:
        raise

Prevention

When it happens

Trigger: Instantiating LangchainPromptTemplate(...) (directly or via from_template) in an environment without the langchain extra; also when the installed langchain version no longer exports ConditionalPromptSelector, breaking the bridge.

Common situations: Core-only installs; langchain major-version upgrades that moved/removed ConditionalPromptSelector; mismatched langchain-core/langchain pins.

Related errors


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