Skip to content

fix: keep a runtime variable that a Jinja set reads into its own default - #13852

Open
Luckyou-d wants to merge 1 commit into
deepset-ai:mainfrom
Luckyou-d:fix/jinja-set-reads-runtime-variable
Open

Luckyou-d wants to merge 1 commit into
deepset-ai:mainfrom
Luckyou-d:fix/jinja-set-reads-runtime-variable

Conversation

@Luckyou-d

@Luckyou-d Luckyou-d commented Nov 21, 2026 •

Copy link
Copy Markdown

Problem

{% set quantity = quantity | default(1) %} exists to default a value the caller supplies, but the prompt builders asked for no quantity at all, so the caller's 12 never reached the template and the default won: Agent.run(quantity=12) sent "1 cartons" to the model.

_collect_assigned_variables subtracted every name a {% set %} binds:

for node in assignments:
    if isinstance(node.target, nodes.Name):
        assigned_variables.add(node.target.name)

Jinja itself already reports such a name as undeclared - the read happens before the binding - so template_variables - assigned_variables removed exactly the caller's own value:

template: "{% set quantity = quantity | default(1) %}Order {{ quantity }} cartons."
undeclared: ['quantity']   assigned: ['quantity']   provided: []      # before
undeclared: ['quantity']   assigned: []             provided: ['quantity']  # after

Change

A bound name is subtracted only when the assignment does not read it:

template provided before provided after
{% set quantity = quantity | default(1) %}{{ quantity }} none quantity
{% set a, b = a or 1, 2 %} none a
{% set q = 12 %}{{ q }} none none
{% set quantity %}x{% endset %} none none
{% if flag %}{% set y = 1 %}{% endif %}{{ y }} flag flag

The last two are what the subtraction was for - a value the template itself provides, and a set that runs in a branch but is read outside it - and they are unchanged. Both {% set x = ... %} and {% set x %}...{% endset %} are handled.

Verification

  • 2 new tests in test/components/builders/test_chat_prompt_builder.py: the self-reading set keeps quantity in the builder's inputs and variables; the plain set stays the template's own. The first fails on main and passes here.
  • pytest test/components/builders/ test/components/converters/test_output_adapter.py test/utils/test_jinja2_extensions.py -> 199 passed.
  • ruff check and ruff format --check clean on both files.
  • Not run here: the issue's end-to-end repro needs a DEEPSEEK_API_KEY and a live model. The fix is in the variable extraction the Agent's prompt builder runs, and the tests assert the variable list that path produces.

Fixes #13770

`{% set quantity = quantity | default(1) %}` exists to default a value the
caller supplies, but the prompt builders asked for no `quantity` at all: the
variable extraction subtracted every name a `{% set %}` binds, so the caller's
12 never reached the template and the default won. An `Agent.run(quantity=12)`
then sent "1 cartons" to the model (deepset-ai#13770).

Subtract a bound name only when the assignment does not read it. Jinja itself
already reports such a name as undeclared - the read happens before the binding
- so what was dropped was the caller's own value; an assignment that reads its
target (`{% set a, b = a or 1, 2 %}`) keeps `a` the same way. A `{% set %}` whose
value does not mention its target stays the template's own, and so does a set
that runs in a branch and is read outside it, which is what the subtraction was
there for.
@Luckyou-d
Luckyou-d requested a review from a team as a code owner November 21, 2026 07:47
@Luckyou-d
Luckyou-d requested review from sjrl and removed request for a team November 21, 2026 07:47
@vercel

vercel Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

@Luckyou-d 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 P2 Medium priority, add to the next sprint if no P1 available topic:tests labels Nov 21, 2026

This branch has not been deployed

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

Labels

P2 Medium priority, add to the next sprint if no P1 available topic:tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Agent silently drops a runtime variable read on the right-hand side of a Jinja set assignment

1 participant