Jinja in LLM node #5268

Closed
opened 2026-02-21 18:10:11 -05:00 by yindo · 6 comments
Owner

Originally created by @l544301590 on GitHub (Aug 28, 2024).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.7.2

Cloud or Self Hosted

Cloud

Steps to reproduce

  1. Use an LLM node in workflow with Jinja enabled
    image

  2. Mock the variable segments
    image

  3. Run the workflow and check the input of LLM node
    image


Here is the DSL file: abc.yml.txt

✔️ Expected Behavior

image

Actual Behavior

image
Originally created by @l544301590 on GitHub (Aug 28, 2024). ### Self Checks - [X] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.7.2 ### Cloud or Self Hosted Cloud ### Steps to reproduce 1. Use an LLM node in workflow with Jinja enabled <img width="832" alt="image" src="https://github.com/user-attachments/assets/9d5e5734-15a1-4f97-a5ed-06ceea53783c"> 2. Mock the variable `segments` <img width="871" alt="image" src="https://github.com/user-attachments/assets/9a37ee4d-08f2-4fe7-b07c-4b9183ac39e3"> 3. Run the workflow and check the input of LLM node <img width="418" alt="image" src="https://github.com/user-attachments/assets/fc5614fe-960a-4cfa-8fbf-a140c0a2a845"> --- Here is the DSL file: [abc.yml.txt](https://github.com/user-attachments/files/16777223/abc.yml.txt) ### ✔️ Expected Behavior <img width="927" alt="image" src="https://github.com/user-attachments/assets/9a206e7d-8209-41b4-9e89-5c2e813c8d5f"> ### ❌ Actual Behavior <img width="408" alt="image" src="https://github.com/user-attachments/assets/1de2cd29-2288-47a8-a760-4ac5128314d7">
yindo added the 💪 enhancement🐞 bug labels 2026-02-21 18:10:11 -05:00
yindo closed this issue 2026-02-21 18:10:11 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 28, 2024):

To resolve the issue where the LLM node with Jinja enabled does not produce the expected output in Dify version 0.7.2, follow these steps:

  1. Enable Jinja-2 Templates: Ensure that the Jinja-2 template language is enabled in the advanced settings of the LLM node.

  2. Correct Template Syntax: Verify that the Jinja-2 template syntax used in your prompts is correct. Refer to the official Jinja documentation for guidance on proper syntax and usage.

  3. Variable Insertion: Ensure that you are correctly inserting variables into the prompt. You can call out the variable insertion menu by typing "/" or "{" to insert special variable blocks or upstream node variables into the prompt as context content.

Example of a Jinja-2 template in the prompt editor:

{% for segment in segments %}
    {{ segment.content }}
{% endfor %}
  1. Context Variables: If you are using context variables, ensure they are properly configured and associated with the correct upstream node variables. Context variables are used to insert external text content retrieved from other nodes into the prompt.

  2. Fetch Jinja Inputs: Ensure that the _fetch_jinja_inputs method in the LLMNode class is correctly fetching and processing Jinja inputs from the variable pool. This method should correctly parse and handle different types of variables (strings, lists, dictionaries, etc.) and integrate them into the prompt.

  3. Model Configuration: Ensure that you have selected the appropriate model and configured its parameters correctly. Dify supports various models like OpenAI's GPT series, Anthropic's Claude series, and Google's Gemini series. Choose a model based on its inference capabilities, cost, response speed, and context window.

  4. Advanced Settings: Utilize advanced settings such as memory, memory window, and conversation role name settings to enhance the LLM's understanding and response accuracy.

  5. Prompt Generator: If you are struggling to create effective prompts, use the prompt generator feature to quickly generate suitable prompt words for your business scenario.

By following these steps, you should be able to resolve the issue and achieve the expected output from the LLM node with Jinja enabled [1][2][3][4].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Aug 28, 2024): <!-- Answer --> To resolve the issue where the LLM node with Jinja enabled does not produce the expected output in Dify version 0.7.2, follow these steps: 1. **Enable Jinja-2 Templates**: Ensure that the Jinja-2 template language is enabled in the advanced settings of the LLM node. 2. **Correct Template Syntax**: Verify that the Jinja-2 template syntax used in your prompts is correct. Refer to the [official Jinja documentation](https://jinja.palletsprojects.com/en/3.1.x/templates/) for guidance on proper syntax and usage. 3. **Variable Insertion**: Ensure that you are correctly inserting variables into the prompt. You can call out the variable insertion menu by typing **"/"** or **"{"** to insert special variable blocks or upstream node variables into the prompt as context content. Example of a Jinja-2 template in the prompt editor: ```jinja {% for segment in segments %} {{ segment.content }} {% endfor %} ``` 4. **Context Variables**: If you are using context variables, ensure they are properly configured and associated with the correct upstream node variables. Context variables are used to insert external text content retrieved from other nodes into the prompt. 5. **Fetch Jinja Inputs**: Ensure that the `_fetch_jinja_inputs` method in the `LLMNode` class is correctly fetching and processing Jinja inputs from the variable pool. This method should correctly parse and handle different types of variables (strings, lists, dictionaries, etc.) and integrate them into the prompt. 6. **Model Configuration**: Ensure that you have selected the appropriate model and configured its parameters correctly. Dify supports various models like OpenAI's GPT series, Anthropic's Claude series, and Google's Gemini series. Choose a model based on its inference capabilities, cost, response speed, and context window. 7. **Advanced Settings**: Utilize advanced settings such as memory, memory window, and conversation role name settings to enhance the LLM's understanding and response accuracy. 8. **Prompt Generator**: If you are struggling to create effective prompts, use the prompt generator feature to quickly generate suitable prompt words for your business scenario. By following these steps, you should be able to resolve the issue and achieve the expected output from the LLM node with Jinja enabled <sup>[[1]](https://github.com/langgenius/dify-docs/blob/main/en/guides/workflow/node/llm.md)</sup><sup>[[2]](https://github.com/langgenius/dify/blob/main/api/core/workflow/nodes/llm/llm_node.py)</sup><sup>[[3]](https://github.com/langgenius/dify-docs/blob/main/zh_CN/guides/workflow/node/llm.md)</sup><sup>[[4]](https://github.com/langgenius/dify-docs/blob/main/jp/guides/workflow/node/llm.md)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@MH4GF commented on GitHub (Sep 1, 2024):

@crazywoola I am interested in contributing to this issue😄 can I submit a PR?

@MH4GF commented on GitHub (Sep 1, 2024): @crazywoola I am interested in contributing to this issue😄 can I submit a PR?
Author
Owner

@crazywoola commented on GitHub (Sep 4, 2024):

Yes, of course you can.

@crazywoola commented on GitHub (Sep 4, 2024): Yes, of course you can.
Author
Owner

@MH4GF commented on GitHub (Sep 8, 2024):

🏃🏻‍♂️📝

https://github.com/langgenius/dify/blob/b1918dae5ecc4270266dcbcfad493f265d5f907a/api/tests/integration_tests/workflow/nodes/__mock/code_executor.py#L20-L21

@MH4GF commented on GitHub (Sep 8, 2024): 🏃🏻‍♂️📝 - Reproduce this situation and add a test that fails: https://github.com/langgenius/dify/pull/8088/commits/9f58060fada03449bb8a27868c8cba99adb46cb9 - https://github.com/langgenius/dify/actions/runs/10757225351/job/29831027433?pr=8088#step:13:243 - The CodeExecutor in the test code has been replaced by a mock, but in the case of Jinja2 the library is executed directly, so the results are probably the same as in the production environment. https://github.com/langgenius/dify/blob/b1918dae5ecc4270266dcbcfad493f265d5f907a/api/tests/integration_tests/workflow/nodes/__mock/code_executor.py#L20-L21
Author
Owner

@crazywoola commented on GitHub (Sep 8, 2024):

🏃🏻‍♂️📝

https://github.com/langgenius/dify/blob/b1918dae5ecc4270266dcbcfad493f265d5f907a/api/tests/integration_tests/workflow/nodes/__mock/code_executor.py#L20-L21

@laipz8200 Could you take a look at this?

@crazywoola commented on GitHub (Sep 8, 2024): > 🏃🏻‍♂️📝 > > * Reproduce this situation and add a test that fails: [9f58060](https://github.com/langgenius/dify/commit/9f58060fada03449bb8a27868c8cba99adb46cb9) > > * https://github.com/langgenius/dify/actions/runs/10757225351/job/29831027433?pr=8088#step:13:243 > * The CodeExecutor in the test code has been replaced by a mock, but in the case of Jinja2 the library is executed directly, so the results are probably the same as in the production environment. > > https://github.com/langgenius/dify/blob/b1918dae5ecc4270266dcbcfad493f265d5f907a/api/tests/integration_tests/workflow/nodes/__mock/code_executor.py#L20-L21 @laipz8200 Could you take a look at this?
Author
Owner

@MH4GF commented on GitHub (Sep 8, 2024):

Apparently, this is because [{'id': '1', 'content': 'abc'}, {'id': '2', 'content': 'def'}] is being passed to Jinja2 inputs as a string rather than an array.

Also https://j2live.ttl255.com/, when value was set to a string, the result was similarly just a line break.

CleanShot 2024-09-08 at 14 39 26@2x


It would seem that a process to parse to an array would be a good way to do this, but that is a risky action and it seems difficult for me, as a newcomer, to make a decision.
After this, it is left to the maintainer! @crazywoola ( @laipz8200 )

@MH4GF commented on GitHub (Sep 8, 2024): Apparently, this is because `[{'id': '1', 'content': 'abc'}, {'id': '2', 'content': 'def'}]` is being passed to Jinja2 inputs as a string rather than an array. Also https://j2live.ttl255.com/, when value was set to a string, the result was similarly just a line break. ![CleanShot 2024-09-08 at 14 39 26@2x](https://github.com/user-attachments/assets/ef254cea-0b34-4288-96e1-5afa6e170b19) --- It would seem that a process to parse to an array would be a good way to do this, but that is a risky action and it seems difficult for me, as a newcomer, to make a decision. After this, it is left to the maintainer! @crazywoola ( @laipz8200 )
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#5268