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
- 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.
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
- In wrappers, make template a required positional argument.
- Prefer passing template; use selector only for LLM-conditional prompt selection.
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
- Must provide either prompt or prompt_template_str.
- Must provide either prompt or prompt_template_str.
- Invalid metric name: {metric}
- Cannot specify both similarity_fn and similarity_mode
- Unknown oversized document strategy: {strategy}
AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15).
Data as JSON: /api/errors/1c47a4d5ca00c88e.
Report an issue: GitHub.