feat(core): support for adding PromptTemplates with formats other than f-string (#32253)

Allow adding`PromptTemplate`s with formats other than `f-string`. Fixes
#32151

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
This commit is contained in:
Sadra Barikbin
2025-09-08 22:46:54 +03:30
committed by GitHub
parent 390606c155
commit 3486d6c74d
2 changed files with 59 additions and 8 deletions

View File

@@ -152,11 +152,8 @@ class PromptTemplate(StringPromptTemplate):
"""
# Allow for easy combining
if isinstance(other, PromptTemplate):
if self.template_format != "f-string":
msg = "Adding prompt templates only supported for f-strings."
raise ValueError(msg)
if other.template_format != "f-string":
msg = "Adding prompt templates only supported for f-strings."
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)
@@ -174,11 +171,14 @@ class PromptTemplate(StringPromptTemplate):
template=template,
input_variables=input_variables,
partial_variables=partial_variables,
template_format="f-string",
template_format=self.template_format,
validate_template=validate_template,
)
if isinstance(other, str):
prompt = PromptTemplate.from_template(other)
prompt = PromptTemplate.from_template(
other,
template_format=self.template_format,
)
return self + prompt
msg = f"Unsupported operand type for +: {type(other)}"
raise NotImplementedError(msg)