From 10e316f021baa91ccd5745be53d8c78d37c72639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Rodr=C3=ADguez?= Date: Fri, 20 Sep 2024 19:33:06 -0400 Subject: [PATCH] core: fix "template" not allowed as prompt param (#26060) - **Description:** fix "template" not allowed as prompt param - **Issue:** #26058 - **Dependencies:** none - [ ] **Add tests and docs**: If you're adding a new integration, please include 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/docs/integrations` directory. - [ ] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ Additional guidelines: - Make sure optional dependencies are imported within a function. - Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests. - Most PRs should not touch more than one package. - Changes should be backwards compatible. - If you are adding something to community, do not re-import it in langchain. If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17. --------- Co-authored-by: Erick Friis --- libs/core/langchain_core/prompts/string.py | 4 ++-- .../tests/unit_tests/prompts/test_prompt.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/libs/core/langchain_core/prompts/string.py b/libs/core/langchain_core/prompts/string.py index bc412cf727487..cb46d452aac25 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.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 e0fb05f11a589..7918658673478 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." + + +@pytest.mark.requires("jinja2") +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."