Skip to content

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

Open

Description

@master1018

Describe the bug
A clear and concise description of what the bug is.
My supplied quantity of 12 cartons becomes 1 carton before the Agent sends the purchase request to the model.

I use a normal Jinja fallback assignment to keep an optional purchasing quantity default in one place: {% set quantity = quantity | default(1) %}. I supply quantity=12 through public Agent.run.

Error message
Error that was thrown (if available)

Rendered first user message: Prepare a purchase order for 1 cartons of paper. State the quantity explicitly.
The completed purchase order explicitly states one (1) carton.
    assert '12 cartons' in result['messages'][0].text, 'The supplied purchase quantity was silently dropped'
AssertionError: The supplied purchase quantity was silently dropped

Expected behavior
Order 12 cartons of paper.

Additional context
Add any other context about the problem here, like document types / preprocessing steps / settings of reader etc.

To Reproduce
Steps to reproduce the behavior

  1. Config LLM model
  2. Run the following python code:
import os
from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.utils import Secret

for key in ('HTTP_PROXY','HTTPS_PROXY','ALL_PROXY','http_proxy','https_proxy','all_proxy'):
    os.environ.pop(key, None)
model = OpenAIChatGenerator(api_key=Secret.from_env_var('DEEPSEEK_API_KEY'), api_base_url='https://api.deepseek.com/v1', model='deepseek-flash', generation_kwargs={'temperature':0,'extra_body':{'thinking':{'type':'disabled'}}})
agent = Agent(chat_generator=model, user_prompt='{% set quantity = quantity | default(1) %}Prepare a purchase order for {{ quantity }} cartons of paper. State the quantity explicitly.')
result = agent.run(messages=[], quantity=12)
print(result)
assert result['exit_reason'] == 'text'
assert '12 cartons' in result['messages'][0].text, 'The supplied purchase quantity was silently dropped'

FAQ Check

System:

  • OS: Linux 5.15.0-139-generic, x86_64, glibc 2.31.
  • Haystack version: 3.5.0

Activity

  1. added theissue type on Nov 16, 2026
  2. its-amann commented on Nov 16, 2026

    @its-amann
    Contributor

    i can take this. plan is to only drop a {% set %} name from the builder variables when it isn't read before it's assigned, so quantity stays a real input in both ChatPromptBuilder and PromptBuilder, plus an Agent test with the default(1) fallback

  3. sunhouy commented on Nov 17, 2026

    @sunhouy
    Contributor

    Landed on something worth checking while reproducing this: the {% set %} target itself is what drops the input, and _collect_assigned_variables does not care whether the assignment body reads the name first.

    With a stub chat generator that records the prompt it gets, your exact user_prompt:

    agent.run(messages=[], quantity=12)
    # before: 'Prepare a purchase order for 1 cartons of paper.'
    #         and `quantity` is not among the Agent's inputs at all
    # after:  'Prepare a purchase order for 12 cartons of paper.'

    Fix I have ready is ~15 lines in haystack/utils/jinja2_extensions.py: keep a name out of the assigned set when its own {% set %} body reads it ({% set quantity = quantity | default(1) %}, {% set q = q | trim %}, tuple targets included), and leave literal assignments ({% set total = 1 + 1 %}) internal as before. PromptBuilder, ChatPromptBuilder, OutputAdapter and ConditionalRouter all derive their inputs from the same helper, so they all get the socket back.

    Tests: unit cases in test/utils/test_jinja2_extensions.py and test/components/builders/test_prompt_builder.py fail before / pass after; test/components/builders test/components/routers test/components/converters/test_output_adapter.py test/utils test/components/agents is 1290 passed, 6 skipped. Release note added.

    Branch is up on my fork: sunhouy/haystack:fix/set-self-read-variable (one commit, deepset-ai/haystack compares it as ahead_by=1). I cannot open the PR from this account, though: the REST endpoint answers 404 and GraphQL answers FORBIDDEN: sunhouy does not have the correct permissions to execute CreatePullRequest, while the same two calls on another repo return 422 / 403 with normal permission errors. Looks account-level rather than a payload problem. @its-amann since you said you would take this one - the branch is there if you would rather pick it up than have me come back to it later.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions