The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
core: prompt variable error msg (#25787)
Erick Friisc8b8335b8289
diff --git a/libs/core/langchain_core/prompts/base.py b/libs/core/langchain_core/prompts/base.pyindex 66c65b9b5a..5b4951e6d1 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_inputdef _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.pyindex 0d18d20132..a9bee11ba8 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])