langchain-ai/langchain · error · NotImplementedError
Unsupported operand type for +: {type(other)}
Error message
Unsupported operand type for +: {type(other)} What it means
Raised by `PromptTemplate.__add__` when the right operand is neither a `PromptTemplate` nor a `str`. LangChain only knows how to concatenate those two types, so anything else (an int, list, another prompt class like `ChatPromptTemplate`, a `Runnable`) triggers `NotImplementedError`. Note the message interpolates the operand's type, e.g. `Unsupported operand type for +: <class 'int'>`.
Source
Thrown at libs/core/langchain_core/prompts/prompt.py:184
if k in partial_variables:
msg = "Cannot have same variable partialed twice."
raise ValueError(msg)
partial_variables[k] = v
return PromptTemplate(
template=template,
input_variables=input_variables,
partial_variables=partial_variables,
template_format=self.template_format,
validate_template=validate_template,
)
if isinstance(other, str):
prompt = PromptTemplate.from_template(
other,
template_format=self.template_format,
)
return self + prompt
msg = f"Unsupported operand type for +: {type(other)}"
raise NotImplementedError(msg)
@property
def _prompt_type(self) -> str:
"""Return the prompt type key."""
return "prompt"
def format(self, **kwargs: Any) -> str:
"""Format the prompt with the inputs.
Args:
**kwargs: Any arguments to be passed to the prompt template.
Returns:
A formatted string.
"""
kwargs = self._merge_partial_and_user_variables(**kwargs)
return DEFAULT_FORMATTER_MAPPING[self.template_format](self.template, **kwargs)
View on GitHub (pinned to e32fa9a52e)
Solutions
- Convert the right operand to a `PromptTemplate` first: `prompt + PromptTemplate.from_template(str(other))`
- For chat prompts, use `ChatPromptTemplate.from_messages([...])` instead of `+` on a `PromptTemplate`
- For plain strings no conversion is needed — check that the value is actually a `str` and not `None` (a stray `None` return is a frequent cause)
Example fix
# before
prompt = PromptTemplate.from_template("Q: {question}\n")
extra = some_fn() # returns None or a non-str object
combined = prompt + extra # NotImplementedError
# after
extra = some_fn() or ""
assert isinstance(extra, str)
combined = prompt + extra # str path is supported Defensive patterns
Strategy: type-guard
Validate before calling
def add_to_prompt(prompt: PromptTemplate, other: object) -> PromptTemplate:
if isinstance(other, PromptTemplate):
return prompt + other
if isinstance(other, str):
return prompt + other
msg = f"cannot concatenate {type(other).__name__} to PromptTemplate"
raise TypeError(msg) Type guard
from langchain_core.prompts import PromptTemplate
def is_addable(other: object) -> bool:
return isinstance(other, (PromptTemplate, str)) and other is not None Try / catch
try:
combined = prompt + other
except NotImplementedError:
combined = prompt + PromptTemplate.from_template(str(other)) Prevention
- Type-hint helper APIs as `PromptTemplate | str` so callers cannot pass odd types
- Watch for functions returning None where a str was expected (missing return statement)
- Use ChatPromptTemplate.from_messages for chat-style composition instead of +
When it happens
Trigger: `prompt + 5`, `prompt + ["a", "b"]`, or `prompt + chat_prompt` where `chat_prompt` is a `ChatPromptTemplate` (which is a `RunnableSequence`, not a `PromptTemplate`). The isinstance checks for `PromptTemplate` and `str` both fail before the raise.
Common situations: Trying to append a chat-style prompt or a `RunnableLambda` to a string prompt with `+`; assuming all prompt classes are mutually addable; concatenating a formatted string that is actually `None` (e.g. the result of a function that forgot to return).
Related errors
- Cannot add templates of different formats
- Unsupported operand type for +: {type(other)}
- Cannot have same variable partialed twice.
- Structured prompts need to be piped to a language model.
- AsyncTextProjection received a non-string final value
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/604742ccf82b42f6.
Report an issue: GitHub.