mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-20 22:03:52 +00:00
core: prompt variable error msg (#25787)
This commit is contained in:
parent
ff168aaec0
commit
c8b8335b82
@ -142,11 +142,18 @@ class BasePromptTemplate(
|
|||||||
)
|
)
|
||||||
missing = set(self.input_variables).difference(inner_input)
|
missing = set(self.input_variables).difference(inner_input)
|
||||||
if missing:
|
if missing:
|
||||||
raise KeyError(
|
msg = (
|
||||||
f"Input to {self.__class__.__name__} is missing variables {missing}. "
|
f"Input to {self.__class__.__name__} is missing variables {missing}. "
|
||||||
f" Expected: {self.input_variables}"
|
f" Expected: {self.input_variables}"
|
||||||
f" Received: {list(inner_input.keys())}"
|
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
|
return inner_input
|
||||||
|
|
||||||
def _format_prompt_with_error_handling(self, inner_input: Dict) -> PromptValue:
|
def _format_prompt_with_error_handling(self, inner_input: Dict) -> PromptValue:
|
||||||
|
@ -702,3 +702,15 @@ def test_prompt_falsy_vars(
|
|||||||
expected if not isinstance(expected, dict) else expected[template_format]
|
expected if not isinstance(expected, dict) else expected[template_format]
|
||||||
)
|
)
|
||||||
assert result.to_string() == expected_output
|
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])
|
||||||
|
Loading…
Reference in New Issue
Block a user