Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions haystack/utils/jinja2_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,25 @@ def _collect_assigned_variables(ast: nodes.Template) -> set[str]:
]

for node in assignments:
targets: set[str] = set()
if isinstance(node.target, nodes.Name):
assigned_variables.add(node.target.name)
targets.add(node.target.name)
elif isinstance(node.target, (nodes.List, nodes.Tuple)):
for name_node in node.target.items:
if isinstance(name_node, nodes.Name):
assigned_variables.add(name_node.name)
targets.add(name_node.name)

# A target that its own assignment reads (e.g. {% set query = query | trim %})
# is not purely template-internal: the value comes from the caller, so the
# name must stay a template input instead of being subtracted as assigned.
value_nodes: list[nodes.Node] = [node.node] if isinstance(node, nodes.Assign) else list(node.body)
read_names = {
name_node.name
for value_node in value_nodes
for name_node in value_node.find_all(nodes.Name)
if name_node.ctx == "load"
}
assigned_variables |= targets - read_names

return assigned_variables

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
fixes:
- |
Fixed ``PromptBuilder``, ``ChatPromptBuilder``, and ``OutputAdapter`` dropping a template
input when the template reassigns it with a self-referencing ``{% set %}`` statement, for
example ``{% set query = query | trim %}`` or ``{% set documents = documents[:3] %}``.
Such variables are now correctly detected as inputs of the component instead of being
treated as purely template-internal, which previously raised
``ValueError: Input 'query' not found`` in pipelines.
6 changes: 6 additions & 0 deletions test/components/builders/test_chat_prompt_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,12 @@ def test_variables_correct_with_assignment(self):
res = builder.run(existing_documents=None)
assert res["prompt"][0].text == "The number is 0!"

def test_variables_correct_with_self_referencing_assignment(self):
template = [ChatMessage.from_user("{% set documents = documents[:3] %}{{ documents }}")]
builder = ChatPromptBuilder(template=template, required_variables="*")
assert builder.variables == ["documents"]
assert builder.required_variables == "*"

def test_variables_correct_with_tuple_assignment(self):
template = """{% message role="user" %}
{% if name is not none -%}
Expand Down
14 changes: 14 additions & 0 deletions test/components/builders/test_prompt_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,20 @@ def test_variables_correct_with_block_assignment(self):
res = builder.run(existing_documents=None)
assert res["prompt"] == "Found no documents"

def test_variables_correct_with_self_referencing_assignment(self):
template = "{% set query = query | trim %}Q: {{ query }}"
builder = PromptBuilder(template=template, required_variables="*")
assert builder.variables == ["query"]
assert builder.required_variables == "*"
res = builder.run(query=" hello ")
assert res["prompt"] == "Q: hello"

def test_variables_correct_with_self_referencing_slice_assignment(self):
template = "{% set documents = documents[:3] %}{{ documents }}"
builder = PromptBuilder(template=template, required_variables="*")
assert builder.variables == ["documents"]
assert builder.required_variables == "*"


class TestPromptBuilderWithJinja2TimeExtension:
@pytest.fixture(autouse=True)
Expand Down
9 changes: 9 additions & 0 deletions test/components/converters/test_output_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,12 @@ def test_variables_correct_with_assignment(self) -> None:
}
res = adapter.run(control="something")
assert res["output"] == 1

def test_variables_correct_with_self_referencing_assignment(self) -> None:
template = "{% set documents = documents[:2] %}{{ documents }}"
adapter = OutputAdapter(template=template, output_type=list)
assert adapter.__haystack_input__._sockets_dict == { # type: ignore[attr-defined]
"documents": InputSocket(name="documents", type=Any)
}
res = adapter.run(documents=[1, 2, 3])
assert res["output"] == [1, 2]
24 changes: 24 additions & 0 deletions test/utils/test_jinja2_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from jinja2 import Environment

from haystack.utils import Jinja2TimeExtension
from haystack.utils.jinja2_extensions import _extract_template_variables_and_assignments


class TestJinja2TimeExtension:
Expand Down Expand Up @@ -132,3 +133,26 @@ def test_invalid_offset(self, jinja_extension: Jinja2TimeExtension) -> None:
def test_invalid_operator(self, jinja_extension: Jinja2TimeExtension) -> None:
with pytest.raises(ValueError, match="Invalid offset or operator"):
jinja_extension._get_datetime("UTC", operator="*", offset="hours=2")


class TestExtractTemplateVariablesAndAssignments:
def test_self_referencing_assignment_is_not_collected_as_assigned(self) -> None:
assigned, variables = _extract_template_variables_and_assignments(
Environment(), "{% set query = query | trim %}Q: {{ query }}"
)
assert assigned == set()
assert variables == {"query"}

def test_pure_assignment_is_collected_as_assigned(self) -> None:
assigned, variables = _extract_template_variables_and_assignments(
Environment(), "{% set x = 1 %}{{ x }} {{ name }}"
)
assert assigned == {"x"}
assert variables == {"name"}

def test_assignment_reading_another_variable_is_collected_as_assigned(self) -> None:
assigned, variables = _extract_template_variables_and_assignments(
Environment(), "{% set x = documents|length %}{{ x }}"
)
assert assigned == {"x"}
assert variables == {"documents"}