Skip to content

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

@sjrl

Description

@sjrl

Describe the bug
When using the following PromptBuilder definition with required_variables="*" we erroneously think the variable existing_doc_len is an input parameter despite being set within the Jinja2 template

from haystack.dataclasses import Document
from haystack.components.builders import PromptBuilder

pb = PromptBuilder(
                template="""{% if existing_documents is not none %}
{% set existing_doc_len = existing_documents|length %}
{% else %}
{% set existing_doc_len = 0 %}
{% endif %}
{% for doc in docs %}
<document reference="{{loop.index + existing_doc_len}}">
{{ doc.content }}
</document>
{% endfor %}
""",
                # required_variables=["existing_documents", "docs"],
                required_variables="*"
            )

res = pb.run(existing_documents=None, docs=[Document(content="Sample content 1"), Document(content="Sample content 2")])

Error message

ValueError: Missing required input variables in PromptBuilder: existing_doc_len. Required variables: ['docs', 'existing_doc_len', 'existing_documents']. Provided variables: {'docs', 'existing_documents'}.

Expected behavior
For variables set within the Jinja2 template not to be picked up as required input variables.

Additional context

  • If the above script is run with ["existing_documents", "docs"] then it runs as expected.
  • This bug also probably exists in ChatPromptBuilder

To Reproduce
Run the provided script

Metadata

Metadata

Assignees

Labels

type:bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions