You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Agent silently drops a runtime variable read on the right-hand side of a Jinja set assignment #13770
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
Config LLM model
Run the following python code:
importosfromhaystack.components.agentsimportAgentfromhaystack.components.generators.chatimportOpenAIChatGeneratorfromhaystack.utilsimportSecretforkeyin ('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)
assertresult['exit_reason'] =='text'assert'12 cartons'inresult['messages'][0].text, 'The supplied purchase quantity was silently dropped'
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
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.
Activity
its-amann commentedon Nov 16, 2026
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
sunhouy commentedon Nov 17, 2026
Landed on something worth checking while reproducing this: the
{% set %}target itself is what drops the input, and_collect_assigned_variablesdoes 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: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,OutputAdapterandConditionalRouterall derive their inputs from the same helper, so they all get the socket back.Tests: unit cases in
test/utils/test_jinja2_extensions.pyandtest/components/builders/test_prompt_builder.pyfail before / pass after;test/components/builders test/components/routers test/components/converters/test_output_adapter.py test/utils test/components/agentsis 1290 passed, 6 skipped. Release note added.Branch is up on my fork:
sunhouy/haystack:fix/set-self-read-variable(one commit,deepset-ai/haystackcompares it as ahead_by=1). I cannot open the PR from this account, though: the REST endpoint answers 404 and GraphQL answersFORBIDDEN: 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.