__add__() — langchain Function Reference
Architecture documentation for the __add__() function in prompt.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 21dc52b9_c2ed_f1ec_e44f_ef2212ac4429["__add__()"] b38b43f8_5be7_5b6c_b01e_36ed41e70435["PromptTemplate"] 21dc52b9_c2ed_f1ec_e44f_ef2212ac4429 -->|defined in| b38b43f8_5be7_5b6c_b01e_36ed41e70435 6328e53b_3705_ca29_5da3_cc71fb6aca10["from_template()"] 21dc52b9_c2ed_f1ec_e44f_ef2212ac4429 -->|calls| 6328e53b_3705_ca29_5da3_cc71fb6aca10 style 21dc52b9_c2ed_f1ec_e44f_ef2212ac4429 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/prompts/prompt.py lines 142–184
def __add__(self, other: Any) -> PromptTemplate:
"""Override the `+` operator to allow for combining prompt templates.
Raises:
ValueError: If the template formats are not f-string or if there are
conflicting partial variables.
NotImplementedError: If the other object is not a `PromptTemplate` or str.
Returns:
A new `PromptTemplate` that is the combination of the two.
"""
# Allow for easy combining
if isinstance(other, PromptTemplate):
if self.template_format != other.template_format:
msg = "Cannot add templates of different formats"
raise ValueError(msg)
input_variables = list(
set(self.input_variables) | set(other.input_variables)
)
template = self.template + other.template
# If any do not want to validate, then don't
validate_template = self.validate_template and other.validate_template
partial_variables = dict(self.partial_variables.items())
for k, v in other.partial_variables.items():
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)
Domain
Subdomains
Defined In
Calls
Source
Frequently Asked Questions
What does __add__() do?
__add__() is a function in the langchain codebase, defined in libs/core/langchain_core/prompts/prompt.py.
Where is __add__() defined?
__add__() is defined in libs/core/langchain_core/prompts/prompt.py at line 142.
What does __add__() call?
__add__() calls 1 function(s): from_template.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free