Skip to content

fix: skip missing jinja2 variables instead of raising LLMNodeError - #214

Open
EvanYao826 wants to merge 1 commit into
langgenius:mainfrom
EvanYao826:main
Open

fix: skip missing jinja2 variables instead of raising LLMNodeError#214
EvanYao826 wants to merge 1 commit into
langgenius:mainfrom
EvanYao826:main

Conversation

@EvanYao826

Copy link
Copy Markdown

Fixes langgenius/dify#38655

Summary

When a Jinja2 variable referenced in prompt_config.jinja2_variables is not present in the variable pool (e.g. because a conditional branch was not taken), _fetch_jinja_inputs raises VariableNotFoundError and the workflow fails — even though the Jinja2 template already handles optional variables with constructs like {% if variable is defined %}.

Root Cause

_fetch_jinja_inputs calls _get_required_variable which raises VariableNotFoundError when the variable pool returns None. The sibling code path _render_jinja2_message already handles this correctly by using variable_pool.get() and falling back to empty string.

Fix

Replace the _get_required_variable call in _fetch_jinja_inputs with a direct variable_pool.get() and skip variables that are not in the pool. This aligns with how _render_jinja2_message already handles missing variables.

Test plan

  • Existing test test_fetch_jinja_inputs_raises_for_missing_variable in dify's test suite updated to expect the new (skip) behavior
  • No regressions: _fetch_jinja_inputs is called during LLM node _run() — missing variables are simply omitted from the returned dict, which is used for input collection, not the Jinja2 rendering itself

When a Jinja2 variable referenced in prompt_config.jinja2_variables
is not present in the variable pool (e.g. because a conditional
branch was not taken), the LLM node should not fail with
VariableNotFoundError. The Jinja2 template already handles optional
variables with constructs like {% if variable is defined %}.

Instead of calling _get_required_variable (which raises), use
variable_pool.get() directly and skip missing variables.
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. bug Something isn't working labels Jul 14, 2026
@github-actions

Copy link
Copy Markdown

Thanks for the contribution. Before we can review or merge this pull request, please read our CLA and add this exact comment once: I have read the CLA Document and I hereby sign the CLA


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

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

Labels

bug Something isn't working size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When using conditional branches (if) and Jinja2 templates in combination, Dify may fail directly if the dependent variable does not exist.

1 participant