run-llama/llama_index · error · ValueError

Must provide either template or selector.

Error message

Must provide either template or selector.

What it means

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.

Source

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

        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

        super().__init__(
            selector=selector,
            metadata=metadata,
            kwargs=kwargs,
            template_vars=template_vars,

View on GitHub (pinned to afd0fef371)

Solutions

  1. Pass template=<langchain PromptTemplate> (a selector is created for you).
  2. Or pass selector=ConditionalPromptSelector(default_prompt=...) if you need conditional selection per LLM.
  3. Check wrapper code forwards template explicitly.

Example fix

# before
lc_prompt = LangchainPromptTemplate()
# after
from langchain.prompts import PromptTemplate as LCPrompt
lc_prompt = LangchainPromptTemplate(template=LCPrompt.from_template("Q: {question}"))
Defensive patterns

Strategy: validation

Validate before calling

if selector is None and template is None:
    raise ValueError("LangchainPromptTemplate needs a template or a selector")

Type guard

def has_template_source(template, selector) -> bool:
    return (template is not None) or (selector is not None)

Prevention

When it happens

Trigger: Calling LangchainPromptTemplate() with no arguments, or with only output_parser/metadata while both template and selector are None.

Common situations: Subclassing or wrapping LangchainPromptTemplate and forgetting to forward the template; passing the template under the wrong kwarg; refactoring from PromptTemplate where template was positional.

Related errors


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