Skip to content

Conversation

@srini047
Copy link
Contributor

@srini047 srini047 commented Oct 24, 2025

Related Issues

Proposed Changes:

Previously we checked only the undeclared variables and left the set variables.
Now, we find all the variables assigned using set in the template and push it to the variables list.

This behaviour would be seen in multiple components namely:

  • PromptBuilder
  • ChatPromptBuilder
  • LLMMetadataExtractor
  • LLMDocumentContentExtractor

How did you test it?

Run the exisiting suite and added test case for the this scenario.

Notes for the reviewer

  • Is this logic for addressing the problem is fine.
  • Then, do let me know if there's any other component (other than ones mentioned above) as well that needs to be addressed with this logic.

Checklist

  • I have read the contributors guidelines and the code of conduct
  • I have updated the related issue with new insights and changes
  • I added unit tests and updated the docstrings
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I documented my code
  • I ran pre-commit hooks and fixed any issue

@vercel
Copy link

vercel bot commented Oct 24, 2025

@srini047 is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions bot added topic:tests type:documentation Improvements on the docs labels Oct 24, 2025
@coveralls
Copy link
Collaborator

coveralls commented Oct 24, 2025

Pull Request Test Coverage Report for Build 18847537461

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 92.221%

Totals Coverage Status
Change from base Build 18846783538: 0.002%
Covered Lines: 13468
Relevant Lines: 14604

💛 - Coveralls

@srini047 srini047 marked this pull request as ready for review October 24, 2025 05:41
@srini047 srini047 requested a review from a team as a code owner October 24, 2025 05:41
@srini047 srini047 requested review from mpangrazzi and removed request for a team October 24, 2025 05:41
@sjrl sjrl requested review from sjrl and removed request for mpangrazzi October 24, 2025 08:08
Comment on lines +182 to +187
assigned_variables = set()
for node in ast.find_all((nodes.Assign, nodes.For)):
if hasattr(node.target, "name"):
assigned_variables.add(node.target.name)

variables = list(template_variables - assigned_variables)
Copy link
Contributor

@sjrl sjrl Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the addition, but this doesn't quite cover all the cases we would like. Let's use the following instead which also handles tuple and list unpacking when using set.

Also the nodes.For case doesn't seem to be needed. We don't pick up for-loop variables like doc in your test.

Suggested change
assigned_variables = set()
for node in ast.find_all((nodes.Assign, nodes.For)):
if hasattr(node.target, "name"):
assigned_variables.add(node.target.name)
variables = list(template_variables - assigned_variables)
assigned_variables = set()
for node in ast.find_all((nodes.Assign,)):
if isinstance(node.target, nodes.Name):
assigned_variables.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)
variables = list(template_variables - assigned_variables)

Comment on lines +357 to +358
res = builder.run(docs=docs, existing_documents=None)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also explicitly test the found variables

Suggested change
res = builder.run(docs=docs, existing_documents=None)
builder = PromptBuilder(template=template, required_variables="*")
assert set(builder.variables) == {"docs", "existing_documents"}


assert "<document reference=" in res["prompt"]
assert "Doc 1" in res["prompt"]
assert "Doc 2" in res["prompt"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add a few more tests to test tuple and list unpacking

    def test_variables_correct_with_tuple_assignment(self):
        template = """{% if existing_documents is not none %}
{% set x, y = (existing_documents|length, 1) %}
{% else %}
{% set x, y = (0, 1) %}
{% endif %}
x={{ x }}, y={{ y }}
"""
        builder = PromptBuilder(template=template, required_variables="*")
        assert set(builder.variables) == {"existing_documents"}
        res = builder.run(existing_documents=None)
        assert "x=0, y=1" in res["prompt"]

    def test_variables_correct_with_list_assignment(self):
            template = """{% if existing_documents is not none %}
{% set x, y = [existing_documents|length, 1] %}
{% else %}
{% set x, y = [0, 1] %}
{% endif %}
x={{ x }}, y={{ y }}
"""
            builder = PromptBuilder(template=template, required_variables="*")
            assert set(builder.variables) == {"existing_documents"}
            res = builder.run(existing_documents=None)
            assert "x=0, y=1" in res["prompt"]

@sjrl
Copy link
Contributor

sjrl commented Oct 27, 2025

With the above listed changes I think we could then extend this to the other components you mention. It may be wise to add this as a utility method in haystack/utils/ either in misc.py or a new file called jinja2.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PromptBuilder incorrectly marks internally set Jinja2 variables as required inputs when using `required_variables='*'

3 participants