Skip to content

The agent engineering platform.

core: fix "template" not allowed as prompt param (#26060)

Alejandro Rodríguez4ac9a6f52ce2
diff --git a/libs/core/langchain_core/prompts/string.py b/libs/core/langchain_core/prompts/string.py
index bc412cf727..cb46d452aa 100644
--- a/libs/core/langchain_core/prompts/string.py
+++ b/libs/core/langchain_core/prompts/string.py
@@ -17,7 +17,7 @@ from langchain_core.utils.formatting import formatter
from langchain_core.utils.interactive_env import is_interactive_env
-def jinja2_formatter(template: str, **kwargs: Any) -> str:
+def jinja2_formatter(template: str, /, **kwargs: Any) -> str:
"""Format a template using jinja2.
*Security warning*:
@@ -99,7 +99,7 @@ def _get_jinja2_variables_from_template(template: str) -> set[str]:
return variables
-def mustache_formatter(template: str, **kwargs: Any) -> str:
+def mustache_formatter(template: str, /, **kwargs: Any) -> str:
"""Format a template using mustache.
Args:
diff --git a/libs/core/tests/unit_tests/prompts/test_prompt.py b/libs/core/tests/unit_tests/prompts/test_prompt.py
index e0fb05f11a..7918658673 100644
--- a/libs/core/tests/unit_tests/prompts/test_prompt.py
+++ b/libs/core/tests/unit_tests/prompts/test_prompt.py
@@ -640,3 +640,22 @@ def test_prompt_missing_vars_error() -> None:
# Check helper text has right number of braces
assert "'{{goingtobemissing}}'" in str(e.value.args[0])
+
+
+def test_prompt_with_template_variable_name_fstring() -> None:
+ template = "This is a {template} test."
+ prompt = PromptTemplate.from_template(template, template_format="f-string")
+ assert prompt.invoke({"template": "bar"}).to_string() == "This is a bar test."
+
+
+def test_prompt_with_template_variable_name_mustache() -> None:
+ template = "This is a {{template}} test."
+ prompt = PromptTemplate.from_template(template, template_format="mustache")
+ assert prompt.invoke({"template": "bar"}).to_string() == "This is a bar test."
+
+
+def test_prompt_with_template_variable_name_jinja2() -> None:
+ template = "This is a {{template}} test."
+ prompt = PromptTemplate.from_template(template, template_format="jinja2")
+ assert prompt.invoke({"template": "bar"}).to_string() == "This is a bar test."