From c8b8335b8289fc948d570d4b4d9515ed713059e9 Mon Sep 17 00:00:00 2001 From: Erick Friis Date: Wed, 28 Aug 2024 15:54:00 -0700 Subject: [PATCH] core: prompt variable error msg (#25787) --- libs/core/langchain_core/prompts/base.py | 9 ++++++++- libs/core/tests/unit_tests/prompts/test_prompt.py | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/libs/core/langchain_core/prompts/base.py b/libs/core/langchain_core/prompts/base.py index 66c65b9b5a0..5b4951e6d1a 100644 --- a/libs/core/langchain_core/prompts/base.py +++ b/libs/core/langchain_core/prompts/base.py @@ -142,11 +142,18 @@ class BasePromptTemplate( ) missing = set(self.input_variables).difference(inner_input) if missing: - raise KeyError( + msg = ( f"Input to {self.__class__.__name__} is missing variables {missing}. " f" Expected: {self.input_variables}" f" Received: {list(inner_input.keys())}" ) + example_key = missing.pop() + msg += ( + f"\nNote: if you intended {{{example_key}}} to be part of the string" + " and not a variable, please escape it with double curly braces like: " + f"'{{{{{example_key}}}}}'." + ) + raise KeyError(msg) return inner_input def _format_prompt_with_error_handling(self, inner_input: Dict) -> PromptValue: diff --git a/libs/core/tests/unit_tests/prompts/test_prompt.py b/libs/core/tests/unit_tests/prompts/test_prompt.py index 0d18d20132f..a9bee11ba89 100644 --- a/libs/core/tests/unit_tests/prompts/test_prompt.py +++ b/libs/core/tests/unit_tests/prompts/test_prompt.py @@ -702,3 +702,15 @@ def test_prompt_falsy_vars( expected if not isinstance(expected, dict) else expected[template_format] ) assert result.to_string() == expected_output + + +def test_prompt_missing_vars_error() -> None: + prompt = PromptTemplate.from_template("This is a {foo} {goingtobemissing} test.") + with pytest.raises(KeyError) as e: + prompt.invoke({"foo": "bar"}) + + # Check that the error message contains the missing variable + assert "{'goingtobemissing'}" in str(e.value.args[0]) + + # Check helper text has right number of braces + assert "'{{goingtobemissing}}'" in str(e.value.args[0])