fix(workflow): tolerate absent upstream jinja2 variables in LLM nodes (#38655) - #39166
Closed
natemiller23 wants to merge 1 commit into
Conversation
…langgenius#38655) When a conditional branch feeds an LLM node whose Jinja2 template guards an optional upstream variable with `{% if var is defined and var %}`, Dify crashed before template evaluation with 'Variable <name> not found'. The crash originated in graphon's LLMNode._fetch_jinja_inputs, which raised VariableNotFoundError for every entry in prompt_config.jinja2_variables whose upstream selector was missing from the variable pool. graphon's own renderer already treats a missing variable as an empty string, so the strict pre-check was the only thing failing. Patch graphon's LLMNode._fetch_jinja_inputs at node-registration time (register_nodes) so an absent upstream jinja2 variable is tolerated and skipped instead of raising. User-authored 'is defined' guards now decide whether the branch renders, exactly as the issue requests. - graphon is an external pinned dependency (graphon==0.6.0), so we monkey-patch the live class once rather than fork the package. - Prompt-template inputs, context and memory paths are unchanged (still required). Refs: langgenius#38655 OpenJobs: openjobs.bot | https://openjobs.bot/jobs/b13fc23b-8eff-477a-afa3-79bdcd77f665 | b13fc23b-8eff-477a-afa3-79bdcd77f665
Member
|
Hi @natemiller23, thanks for opening this pull request. Why this is being closedIssue #38655 has multiple open pull requests. Under the duplicate-PR rule, pull requests from the issue author are ranked first, then creation time is used as the FIFO tie-breaker. PR #39153 is the retained pull request, so this one is being closed. Next stepsPlease coordinate on PR #39153 if you would like to contribute further. Thank you for understanding. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #38655 — when a conditional branch feeds an LLM node whose Jinja2 template guards an optional upstream variable with
{% if var is defined and var %}, Dify crashed before template evaluation withVariable <name> not found.The crash originated in graphon's
LLMNode._fetch_jinja_inputs, which raisedVariableNotFoundErrorfor every entry inprompt_config.jinja2_variableswhose upstream selector was missing from the variable pool. graphon's own renderer (_render_jinja2_message) already treats a missing variable as an empty string, so the strict pre-check was the only thing failing.Fix
Patch graphon's
LLMNode._fetch_jinja_inputsat node-registration time (register_nodesinapi/core/workflow/node_factory.py) so an absent upstream jinja2 variable is tolerated and skipped instead of raising. The user-authoredis definedguard now decides whether the branch renders — exactly as the issue requests.graphon==0.6.0), so we monkey-patch the live class once at registration time rather than fork the package.Testing notes
Unit tests added / updated:
api/tests/unit_tests/core/workflow/test_node_factory.py::TestJinjaMissingVariableTolerancetest_absent_jinja_variable_is_tolerated: absent jinja2 variable no longer raisesVariableNotFoundError; present variables still collected.test_only_present_variables_collected: only present upstream variables are returned in the jinja input mapping.api/tests/unit_tests/core/workflow/nodes/llm/test_node.pytest_fetch_jinja_inputs_raises_for_missing_variable->test_fetch_jinja_inputs_tolerates_missing_variableto assert the new intended behavior (returns{}instead of raising).Run with:
Result: all green (the regression suite for the LLM node jinja path passes).
OpenJobs
Refs: #38655