{"record":{"id":"604742ccf82b42f6","repo":"langchain-ai/langchain","slug":"unsupported-operand-type-for-type-other-604742","errorCode":null,"errorMessage":"Unsupported operand type for +: {type(other)}","messagePattern":"Unsupported operand type for \\+: (.+?)","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/prompt.py","lineNumber":184,"sourceCode":"                if k in partial_variables:\n                    msg = \"Cannot have same variable partialed twice.\"\n                    raise ValueError(msg)\n                partial_variables[k] = v\n            return PromptTemplate(\n                template=template,\n                input_variables=input_variables,\n                partial_variables=partial_variables,\n                template_format=self.template_format,\n                validate_template=validate_template,\n            )\n        if isinstance(other, str):\n            prompt = PromptTemplate.from_template(\n                other,\n                template_format=self.template_format,\n            )\n            return self + prompt\n        msg = f\"Unsupported operand type for +: {type(other)}\"\n        raise NotImplementedError(msg)\n\n    @property\n    def _prompt_type(self) -> str:\n        \"\"\"Return the prompt type key.\"\"\"\n        return \"prompt\"\n\n    def format(self, **kwargs: Any) -> str:\n        \"\"\"Format the prompt with the inputs.\n\n        Args:\n            **kwargs: Any arguments to be passed to the prompt template.\n\n        Returns:\n            A formatted string.\n        \"\"\"\n        kwargs = self._merge_partial_and_user_variables(**kwargs)\n        return DEFAULT_FORMATTER_MAPPING[self.template_format](self.template, **kwargs)\n","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/prompt.py#L166-L202","documentation":"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'>`.","triggerScenarios":"`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.","commonSituations":"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).","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)"],"exampleFix":"# before\nprompt = PromptTemplate.from_template(\"Q: {question}\\n\")\nextra = some_fn()          # returns None or a non-str object\ncombined = prompt + extra  # NotImplementedError\n\n# after\nextra = some_fn() or \"\"\nassert isinstance(extra, str)\ncombined = prompt + extra  # str path is supported","handlingStrategy":"type-guard","validationCode":"def add_to_prompt(prompt: PromptTemplate, other: object) -> PromptTemplate:\n    if isinstance(other, PromptTemplate):\n        return prompt + other\n    if isinstance(other, str):\n        return prompt + other\n    msg = f\"cannot concatenate {type(other).__name__} to PromptTemplate\"\n    raise TypeError(msg)","typeGuard":"from langchain_core.prompts import PromptTemplate\n\ndef is_addable(other: object) -> bool:\n    return isinstance(other, (PromptTemplate, str)) and other is not None","tryCatchPattern":"try:\n    combined = prompt + other\nexcept NotImplementedError:\n    combined = prompt + PromptTemplate.from_template(str(other))","preventionTips":["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 +"],"tags":["prompts","prompt-template","notimplementederror","operator-overload","type-mismatch"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}