From 84f8b99cf5086c7e7a65450792474e341aeaf209 Mon Sep 17 00:00:00 2001 From: Sadra Barikbin Date: Sun, 27 Jul 2025 01:47:10 +0330 Subject: [PATCH 1/5] Apply change --- libs/core/langchain_core/prompts/prompt.py | 13 ++++----- .../tests/unit_tests/prompts/test_prompt.py | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/libs/core/langchain_core/prompts/prompt.py b/libs/core/langchain_core/prompts/prompt.py index 6b77a4e71cb..8c9732b2daf 100644 --- a/libs/core/langchain_core/prompts/prompt.py +++ b/libs/core/langchain_core/prompts/prompt.py @@ -137,12 +137,9 @@ class PromptTemplate(StringPromptTemplate): """Override the + operator to allow for combining prompt templates.""" # 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." - raise ValueError(msg) + if self.template_format != other.template_format: + msg = "Cannot add two templates of different formats" + raise ValueError(msg) input_variables = list( set(self.input_variables) | set(other.input_variables) ) @@ -159,11 +156,11 @@ 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) diff --git a/libs/core/tests/unit_tests/prompts/test_prompt.py b/libs/core/tests/unit_tests/prompts/test_prompt.py index e092eb66581..12315e1a264 100644 --- a/libs/core/tests/unit_tests/prompts/test_prompt.py +++ b/libs/core/tests/unit_tests/prompts/test_prompt.py @@ -681,3 +681,30 @@ 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." + + +def test_prompt_template_add_with_with_another_format(): + with pytest.raises(match=r"Cannot add two templates"): + PromptTemplate.from_template("This is a {template}") + PromptTemplate.from_template("So {{this}} is", template_format="mustache") + + +@pytest.mark.parametrize( + "template_format, prompt1, prompt2", + [ + ("f-string", "This is a {variable}", ". This is {another_variable}"), + pytest.param("jinja2", "This is a {{variable}}", ". This is {{another_variable}}", marks=[pytest.mark.requires("jinja2")]), + ("mustache", "This is a {{variable}}", ". This is {{another_variable}}"), + ] +) +def test_prompt_template_add(template_format: str, prompt1: str, prompt2: str) -> None: + first_prompt = PromptTemplate.from_template(prompt1, template_format=template_format) + second_prompt = PromptTemplate.from_template(prompt2, template_format=template_format) + + concated_prompt = first_prompt + second_prompt + prompt_of_concated = PromptTemplate.from_template(prompt1 + prompt2, template_format=template_format) + + assert concated_prompt.input_variables == prompt_of_concated.input_variables + assert ( + concated_prompt.format(variable="template", another_variable="other_template") == + prompt_of_concated.format(variable="template", another_variable="other_template") + ) \ No newline at end of file From 4b74e3553279d4120f827a26c3727daa5a436403 Mon Sep 17 00:00:00 2001 From: Sadra Barikbin Date: Sun, 27 Jul 2025 02:02:00 +0330 Subject: [PATCH 2/5] Apply lint --- libs/core/langchain_core/prompts/prompt.py | 9 ++++++--- libs/core/tests/unit_tests/prompts/test_prompt.py | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/libs/core/langchain_core/prompts/prompt.py b/libs/core/langchain_core/prompts/prompt.py index 8c9732b2daf..36ed47250ce 100644 --- a/libs/core/langchain_core/prompts/prompt.py +++ b/libs/core/langchain_core/prompts/prompt.py @@ -138,8 +138,8 @@ class PromptTemplate(StringPromptTemplate): # Allow for easy combining if isinstance(other, PromptTemplate): if self.template_format != other.template_format: - msg = "Cannot add two templates of different formats" - raise ValueError(msg) + msg = "Cannot add templates of different formats" + raise ValueError(msg) input_variables = list( set(self.input_variables) | set(other.input_variables) ) @@ -160,7 +160,10 @@ class PromptTemplate(StringPromptTemplate): validate_template=validate_template, ) if isinstance(other, str): - prompt = PromptTemplate.from_template(other, template_format=self.template_format) + prompt = PromptTemplate.from_template( + other, + template_format=self.template_format, + ) return self + prompt msg = f"Unsupported operand type for +: {type(other)}" raise NotImplementedError(msg) diff --git a/libs/core/tests/unit_tests/prompts/test_prompt.py b/libs/core/tests/unit_tests/prompts/test_prompt.py index 12315e1a264..3d09882fd69 100644 --- a/libs/core/tests/unit_tests/prompts/test_prompt.py +++ b/libs/core/tests/unit_tests/prompts/test_prompt.py @@ -684,7 +684,7 @@ def test_prompt_with_template_variable_name_jinja2() -> None: def test_prompt_template_add_with_with_another_format(): - with pytest.raises(match=r"Cannot add two templates"): + with pytest.raises(match=r"Cannot add templates"): PromptTemplate.from_template("This is a {template}") + PromptTemplate.from_template("So {{this}} is", template_format="mustache") From d42865a58fd2455d1c9c01ebb54e9cc6a17f8dee Mon Sep 17 00:00:00 2001 From: Sadra Barikbin Date: Sun, 27 Jul 2025 02:13:14 +0330 Subject: [PATCH 3/5] Apply lint --- .../tests/unit_tests/prompts/test_prompt.py | 48 +++++++++++++------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/libs/core/tests/unit_tests/prompts/test_prompt.py b/libs/core/tests/unit_tests/prompts/test_prompt.py index 3d09882fd69..66df3bc5b84 100644 --- a/libs/core/tests/unit_tests/prompts/test_prompt.py +++ b/libs/core/tests/unit_tests/prompts/test_prompt.py @@ -683,28 +683,48 @@ def test_prompt_with_template_variable_name_jinja2() -> None: assert prompt.invoke({"template": "bar"}).to_string() == "This is a bar test." -def test_prompt_template_add_with_with_another_format(): - with pytest.raises(match=r"Cannot add templates"): +def test_prompt_template_add_with_with_another_format() -> None: + with pytest.raises(ValueError, match=r"Cannot add templates"): PromptTemplate.from_template("This is a {template}") + PromptTemplate.from_template("So {{this}} is", template_format="mustache") @pytest.mark.parametrize( - "template_format, prompt1, prompt2", - [ - ("f-string", "This is a {variable}", ". This is {another_variable}"), - pytest.param("jinja2", "This is a {{variable}}", ". This is {{another_variable}}", marks=[pytest.mark.requires("jinja2")]), - ("mustache", "This is a {{variable}}", ". This is {{another_variable}}"), - ] + ("template_format", "prompt1", "prompt2"), + [ + ("f-string", "This is a {variable}", ". This is {another_variable}"), + pytest.param( + "jinja2", + "This is a {{variable}}", + ". This is {{another_variable}}", + marks=[pytest.mark.requires("jinja2")] + ), + ("mustache", "This is a {{variable}}", ". This is {{another_variable}}"), + ] ) def test_prompt_template_add(template_format: str, prompt1: str, prompt2: str) -> None: - first_prompt = PromptTemplate.from_template(prompt1, template_format=template_format) - second_prompt = PromptTemplate.from_template(prompt2, template_format=template_format) + first_prompt = PromptTemplate.from_template( + prompt1, + template_format=template_format, + ) + second_prompt = PromptTemplate.from_template( + prompt2, + template_format=template_format, + ) concated_prompt = first_prompt + second_prompt - prompt_of_concated = PromptTemplate.from_template(prompt1 + prompt2, template_format=template_format) + prompt_of_concated = PromptTemplate.from_template( + prompt1 + prompt2, + template_format=template_format, + ) assert concated_prompt.input_variables == prompt_of_concated.input_variables assert ( - concated_prompt.format(variable="template", another_variable="other_template") == - prompt_of_concated.format(variable="template", another_variable="other_template") - ) \ No newline at end of file + concated_prompt.format( + variable="template", + another_variable="other_template", + ) == + prompt_of_concated.format( + variable="template", + another_variable="other_template", + ) + ) From 4c256fb5b40051296844bc0f34a5508518231ac4 Mon Sep 17 00:00:00 2001 From: Sadra Barikbin Date: Sun, 27 Jul 2025 02:17:43 +0330 Subject: [PATCH 4/5] Apply lint again --- libs/core/tests/unit_tests/prompts/test_prompt.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libs/core/tests/unit_tests/prompts/test_prompt.py b/libs/core/tests/unit_tests/prompts/test_prompt.py index 66df3bc5b84..8ae3156e82d 100644 --- a/libs/core/tests/unit_tests/prompts/test_prompt.py +++ b/libs/core/tests/unit_tests/prompts/test_prompt.py @@ -685,7 +685,10 @@ def test_prompt_with_template_variable_name_jinja2() -> None: def test_prompt_template_add_with_with_another_format() -> None: with pytest.raises(ValueError, match=r"Cannot add templates"): - PromptTemplate.from_template("This is a {template}") + PromptTemplate.from_template("So {{this}} is", template_format="mustache") + ( + PromptTemplate.from_template("This is a {template}") + + PromptTemplate.from_template("So {{this}} is", template_format="mustache") + ) @pytest.mark.parametrize( From 5a1440b9f143c151d7bdd256de8280cc1534f2f9 Mon Sep 17 00:00:00 2001 From: Sadra Barikbin Date: Sun, 27 Jul 2025 02:44:07 +0330 Subject: [PATCH 5/5] Apply lint using uv --- .../tests/unit_tests/prompts/test_prompt.py | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/libs/core/tests/unit_tests/prompts/test_prompt.py b/libs/core/tests/unit_tests/prompts/test_prompt.py index 8ae3156e82d..bc4780e1541 100644 --- a/libs/core/tests/unit_tests/prompts/test_prompt.py +++ b/libs/core/tests/unit_tests/prompts/test_prompt.py @@ -1,7 +1,7 @@ """Test functionality related to prompts.""" import re -from typing import Any, Union +from typing import Any, Literal, Union from unittest import mock import pytest @@ -686,8 +686,8 @@ def test_prompt_with_template_variable_name_jinja2() -> None: def test_prompt_template_add_with_with_another_format() -> None: with pytest.raises(ValueError, match=r"Cannot add templates"): ( - PromptTemplate.from_template("This is a {template}") + - PromptTemplate.from_template("So {{this}} is", template_format="mustache") + PromptTemplate.from_template("This is a {template}") + + PromptTemplate.from_template("So {{this}} is", template_format="mustache") ) @@ -699,12 +699,16 @@ def test_prompt_template_add_with_with_another_format() -> None: "jinja2", "This is a {{variable}}", ". This is {{another_variable}}", - marks=[pytest.mark.requires("jinja2")] + marks=[pytest.mark.requires("jinja2")], ), ("mustache", "This is a {{variable}}", ". This is {{another_variable}}"), - ] + ], ) -def test_prompt_template_add(template_format: str, prompt1: str, prompt2: str) -> None: +def test_prompt_template_add( + template_format: Literal["f-string", "mustache", "jinja2"], + prompt1: str, + prompt2: str, +) -> None: first_prompt = PromptTemplate.from_template( prompt1, template_format=template_format, @@ -721,13 +725,10 @@ def test_prompt_template_add(template_format: str, prompt1: str, prompt2: str) - ) assert concated_prompt.input_variables == prompt_of_concated.input_variables - assert ( - concated_prompt.format( - variable="template", - another_variable="other_template", - ) == - prompt_of_concated.format( - variable="template", - another_variable="other_template", - ) + assert concated_prompt.format( + variable="template", + another_variable="other_template", + ) == prompt_of_concated.format( + variable="template", + another_variable="other_template", )