forked from qodo-benchmark/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
refactor(workflow): add Jinja2 renderer abstraction for template transform #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tomerqodo
wants to merge
6
commits into
coderabbit_full_base_refactorworkflow_add_jinja2_renderer_abstraction_for_template_transform_pr3
Choose a base branch
from
coderabbit_full_head_refactorworkflow_add_jinja2_renderer_abstraction_for_template_transform_pr3
base: coderabbit_full_base_refactorworkflow_add_jinja2_renderer_abstraction_for_template_transform_pr3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+849
−33
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2bf031d
Added constructor-based CodeExecutor injection to TemplateTransformNo…
laipz8200 46b7145
Introduced a Jinja2-specific rendering abstraction and wired Template…
laipz8200 2a616d8
Updated TemplateRenderError to inherit from ValueError and switched n…
laipz8200 64a46c1
chore(lint): ran `make lint` (ruff format updated `api/tests/unit_tes…
laipz8200 3977898
Update api/core/workflow/nodes/template_transform/template_renderer.py
laipz8200 24297d2
update pr
tomerqodo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
40 changes: 40 additions & 0 deletions
40
api/core/workflow/nodes/template_transform/template_renderer.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping | ||
| from typing import Any, Protocol | ||
|
|
||
| from core.helper.code_executor.code_executor import CodeExecutionError, CodeExecutor, CodeLanguage | ||
|
|
||
|
|
||
| class TemplateRenderError(ValueError): | ||
| """Raised when rendering a Jinja2 template fails.""" | ||
|
|
||
|
|
||
| class Jinja2TemplateRenderer(Protocol): | ||
| """Render Jinja2 templates for template transform nodes.""" | ||
|
|
||
| def render_template(self, template: str, variables: Mapping[str, Any]) -> str: | ||
| """Render a Jinja2 template with provided variables.""" | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
| class CodeExecutorJinja2TemplateRenderer(Jinja2TemplateRenderer): | ||
| """Adapter that renders Jinja2 templates via CodeExecutor.""" | ||
|
|
||
| _code_executor: type[CodeExecutor] | ||
|
|
||
| def __init__(self, code_executor: type[CodeExecutor] | None = None) -> None: | ||
| self._code_executor = code_executor or CodeExecutor | ||
|
|
||
| def render_template(self, template: str, variables: Mapping[str, Any]) -> str: | ||
| try: | ||
| result = self._code_executor.execute_workflow_code_template( | ||
| language=CodeLanguage.JINJA2, code=template, inputs=variables | ||
| ) | ||
| except CodeExecutionError as exc: | ||
| raise TemplateRenderError(str(exc)) from exc | ||
|
|
||
| rendered = result.get("result") | ||
| if rendered is not None and not isinstance(rendered, str): | ||
| raise TemplateRenderError("Template render result must be a string.") | ||
| return rendered | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return type mismatch: method can return
Nonebut signature declaresstr.The method signature declares
-> str, butresult.get("result")on line 37 can returnNonewhen the key is absent. Line 40 then returns this potentiallyNonevalue.This causes downstream issues in
template_transform_node.pywherelen(rendered)(line 69) would raiseTypeErrorifrenderedisNone.Either:
str | Noneand handleNonein callers, orTemplateRenderErrorwhen the result is missing/None🐛 Proposed fix (option 2 - enforce non-None return)
rendered = result.get("result") if rendered is not None and not isinstance(rendered, str): raise TemplateRenderError("Template render result must be a string.") + if rendered is None: + raise TemplateRenderError("Template render returned no result.") return rendered📝 Committable suggestion
🤖 Prompt for AI Agents