fix: handle missing Jinja2 variables gracefully in LLM node - #213
Open
EvanYao826 wants to merge 1 commit into
Open
fix: handle missing Jinja2 variables gracefully in LLM node#213EvanYao826 wants to merge 1 commit into
EvanYao826 wants to merge 1 commit into
Conversation
…ariableNotFoundError
Contributor
|
This PR seems related to and potentially conflict with #214. What's the motivation for these two PR @EvanYao826 ? |
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 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_inputsshould return empty strings for missing variables rather than raisingVariableNotFoundError.Changes
src/graphon/nodes/llm/node.py: Changed_fetch_jinja_inputsto usevariable_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 throwingVariableNotFoundError.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.