fix: Match front and backend prompt variable behavior (#8522)

* fix handling of braces in front and back end

* add tests

* ruff check fix

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* [autofix.ci] apply automated fixes

* address pr comments and switches to use formatter

* remove code fence that isn't working

* comment

* [autofix.ci] apply automated fixes

* text fix

* style fix

* test fix

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Mike Fortman 2025-06-18 09:24:39 -05:00 committed by GitHub
commit ddc17d4c77
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 261 additions and 48 deletions

View file

@ -0,0 +1,60 @@
import pytest
from langflow.interface.utils import extract_input_variables_from_prompt
@pytest.mark.parametrize(
("prompt", "expected"),
[
# Basic variable extraction
("Hello {name}!", ["name"]),
("Hi {name}, you are {age} years old", ["name", "age"]),
# Empty prompt
("", []),
("No variables here", []),
# Duplicate variables
("Hello {name}! How are you {name}?", ["name"]),
# Whitespace handling - Formatter preserves whitespace in field names
("Hello { name }!", [" name "]),
("Hi { name }, bye", [" name "]),
# Multiple braces (escaping)
("Escaped {{not_a_var}}", []),
("Mixed {{escaped}} and {real_var}", ["real_var"]),
("Double escaped {{{{not_this}}}}", []),
# Complex cases
("Hello {name}! Your score is {{4 + 5}}, age: {age}", ["name", "age"]),
("Nested {{obj['key']}} with {normal_var}", ["normal_var"]),
("Template {{user.name}} with {id} and {type}", ["id", "type"]),
# Edge cases
("{single}", ["single"]),
("{{double}}", []),
("{{{}}}", []),
# Multiple variables with various spacing
(
"""
Multi-line with {var1}
and {var2} plus
{var3} at the end
""",
["var1", "var2", "var3"],
),
],
)
def test_extract_input_variables(prompt, expected):
"""Test the extract_input_variables_from_prompt function with various cases."""
result = extract_input_variables_from_prompt(prompt)
assert sorted(result) == sorted(expected), f"Failed for prompt: {prompt}"
@pytest.mark.parametrize(
("prompt", "expected_error"),
[
# Malformed format strings that should raise ValueError
("}{", "Single '}' encountered in format string"),
("{incomplete", "expected '}' before end of string"),
("incomplete}", "Single '}' encountered in format string"),
],
)
def test_extract_input_variables_malformed(prompt, expected_error):
"""Test that malformed format strings raise ValueError."""
with pytest.raises(ValueError, match=expected_error):
extract_input_variables_from_prompt(prompt)