Skip to content

Commit

Permalink
core: fix "template" not allowed as prompt param (langchain-ai#26060)
Browse files Browse the repository at this point in the history
- **Description:**  fix "template" not allowed as prompt param
- **Issue:** langchain-ai#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 <erick@langchain.dev>
  • Loading branch information
2 people authored and Sheepsta300 committed Oct 1, 2024
1 parent 8253e1e commit 10e316f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions libs/core/langchain_core/prompts/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -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*:
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions libs/core/tests/unit_tests/prompts/test_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."

0 comments on commit 10e316f

Please sign in to comment.