Skip to content

fix: handle missing Jinja2 variables gracefully in LLM node - #213

Open
EvanYao826 wants to merge 1 commit into
langgenius:mainfrom
EvanYao826:fix/jinja2-variable-not-found
Open

fix: handle missing Jinja2 variables gracefully in LLM node#213
EvanYao826 wants to merge 1 commit into
langgenius:mainfrom
EvanYao826:fix/jinja2-variable-not-found

Conversation

@EvanYao826

Copy link
Copy Markdown

Summary

Fixes langgenius/dify#38655

When an LLM node uses Jinja2 templates with variables that may not exist in the variable pool (e.g., because they come from a branch path that wasn't taken during workflow execution), _fetch_jinja_inputs should return empty strings for missing variables rather than raising VariableNotFoundError.

Changes

  • src/graphon/nodes/llm/node.py: Changed _fetch_jinja_inputs to use variable_pool.get() directly instead of _get_required_variable(). When a variable is not found in the pool, it defaults to an empty string instead of throwing VariableNotFoundError.

Rationale

This is consistent with how _render_jinja2_message (in the same file) already handles missing variables: variable.to_object() if variable else "". The Jinja2 template can use {% if var is defined %} to handle optional variables, but that check never gets reached because the code fails before rendering.

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working labels Jul 10, 2026
@QuantumGhost

QuantumGhost commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

This PR seems related to and potentially conflict with #214. What's the motivation for these two PR @EvanYao826 ?

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:S This PR changes 10-29 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.

2 participants