tongyi model code block mode wrapper is not stable #5329

Closed
opened 2026-02-21 18:10:27 -05:00 by yindo · 1 comment
Owner

Originally created by @zouchengzhuo on GitHub (Sep 1, 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

Self Hosted (Docker)

Steps to reproduce

https://github.com/langgenius/dify/pull/5396
This mr try to resolve this problem, but it works not stable because the llm's answer is not stable. Try to ask the same question for some times in that issue, and you may get empty string in some case.

In fact, the basic class already provides a solution with good compatibility, compatible with markdown and non-markdown format returns.

I'll submit a mr to resolve this problem later.

✔️ Expected Behavior

the json result returns stable with markdown or none-markdown result.
the llm output:
qwen-json-bug-fix-2
the result:
qwen-json-bug-fix

Actual Behavior

the output of llm is not stable, and the json result may be empty string
qwen-json-bug2
get some empty string
qwen-json-bug

Originally created by @zouchengzhuo on GitHub (Sep 1, 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 Self Hosted (Docker) ### Steps to reproduce https://github.com/langgenius/dify/pull/5396 This mr try to resolve this problem, but it works not stable because the llm's answer is not stable. Try to ask the same question for some times in that issue, and you may get empty string in some case. In fact, the basic class already provides a solution with good compatibility, compatible with markdown and non-markdown format returns. I'll submit a mr to resolve this problem later. ### ✔️ Expected Behavior the json result returns stable with markdown or none-markdown result. the llm output: ![qwen-json-bug-fix-2](https://github.com/user-attachments/assets/d40abf60-42f9-42c6-af76-5f02735d4341) the result: ![qwen-json-bug-fix](https://github.com/user-attachments/assets/e6eea4dd-be33-4943-bbbd-f0bb646ff184) ### ❌ Actual Behavior the output of llm is not stable, and the json result may be empty string ![qwen-json-bug2](https://github.com/user-attachments/assets/e92156e2-198b-4911-9217-4e9787bcd411) get some empty string ![qwen-json-bug](https://github.com/user-attachments/assets/c9652529-76a6-426d-9401-ef2e62da48a4)
yindo added the 🐞 bug label 2026-02-21 18:10:27 -05:00
yindo closed this issue 2026-02-21 18:10:27 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 1, 2024):

@zouchengzhuo Hello! I'm here to help you with any bugs, questions, or contributions you have.

To stabilize the JSON output from the Tongyi model code block mode wrapper in Dify version 0.7.2 when using a self-hosted Docker setup, ensure that the response_format parameter is correctly set to "JSON" and that the stop words are properly configured to handle the JSON output.

Here is the relevant part of the _code_block_mode_wrapper method that you need to focus on:

def _code_block_mode_wrapper(self, model: str, credentials: dict,
                             prompt_messages: list[PromptMessage], model_parameters: dict,
                             tools: list[PromptMessageTool] | None = None, stop: list[str] | None = None,
                             stream: bool = True, user: str | None = None, callbacks: list[Callback] = None) \
        -> LLMResult | Generator:
    """
    Wrapper for code block mode
    """
    block_prompts = """You should always follow the instructions and output a valid {{block}} object.
The structure of the {{block}} object you can found in the instructions, use {"answer": "$your_answer"} as the default structure
if you are not sure about the structure.

<instructions>
{{instructions}}
</instructions>
You should also complete the text started with ``` but not tell ``` directly.
"""
    code_block = model_parameters.get("response_format", "")
    if not code_block:
        return self._invoke(
            model=model,
            credentials=credentials,
            prompt_messages=prompt_messages,
            model_parameters=model_parameters,
            tools=tools,
            stop=stop,
            stream=stream,
            user=user
        )

    model_parameters.pop("response_format")
    stop = stop or []
    stop.extend(["\n```", "```\n"])
    block_prompts = block_prompts.replace("{{block}}", code_block)

    # check if there is a system message
    if len(prompt_messages) > 0 and isinstance(prompt_messages[0], SystemPromptMessage):
        # override the system message
        prompt_messages[0] = SystemPromptMessage(
            content=block_prompts
            .replace("{{instructions}}", prompt_messages[0].content)
        )
    else:
        # insert the system message
        prompt_messages.insert(0, SystemPromptMessage(
            content=block_prompts
            .replace("{{instructions}}", f"Please output a valid {code_block} with markdown codeblocks.")
        ))

    if len(prompt_messages) > 0 and isinstance(prompt_messages[-1], UserPromptMessage):
        # add ```JSON\n to the last message
        prompt_messages[-1].content += f"\n```{code_block}\n"
    else:
        # append a user message
        prompt_messages.append(UserPromptMessage(
            content=f"```{code_block}\n"
        ))

    response = self._invoke(
        model=model,
        credentials=credentials,
        prompt_messages=prompt_messages,
        model_parameters=model_parameters,
        tools=tools,
        stop=stop,
        stream=stream,
        user=user
    )

Ensure that the response_format in model_parameters is set to "JSON" and that the stop words include \n````, and ```\n` to properly handle the JSON output. This will help in stabilizing the JSON output from the Tongyi model [1].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Sep 1, 2024): <!-- Greeting --> @zouchengzhuo Hello! I'm here to help you with any bugs, questions, or contributions you have. <!-- Answer --> To stabilize the JSON output from the Tongyi model code block mode wrapper in Dify version 0.7.2 when using a self-hosted Docker setup, ensure that the `response_format` parameter is correctly set to "JSON" and that the stop words are properly configured to handle the JSON output. Here is the relevant part of the `_code_block_mode_wrapper` method that you need to focus on: ```python def _code_block_mode_wrapper(self, model: str, credentials: dict, prompt_messages: list[PromptMessage], model_parameters: dict, tools: list[PromptMessageTool] | None = None, stop: list[str] | None = None, stream: bool = True, user: str | None = None, callbacks: list[Callback] = None) \ -> LLMResult | Generator: """ Wrapper for code block mode """ block_prompts = """You should always follow the instructions and output a valid {{block}} object. The structure of the {{block}} object you can found in the instructions, use {"answer": "$your_answer"} as the default structure if you are not sure about the structure. <instructions> {{instructions}} </instructions> You should also complete the text started with ``` but not tell ``` directly. """ code_block = model_parameters.get("response_format", "") if not code_block: return self._invoke( model=model, credentials=credentials, prompt_messages=prompt_messages, model_parameters=model_parameters, tools=tools, stop=stop, stream=stream, user=user ) model_parameters.pop("response_format") stop = stop or [] stop.extend(["\n```", "```\n"]) block_prompts = block_prompts.replace("{{block}}", code_block) # check if there is a system message if len(prompt_messages) > 0 and isinstance(prompt_messages[0], SystemPromptMessage): # override the system message prompt_messages[0] = SystemPromptMessage( content=block_prompts .replace("{{instructions}}", prompt_messages[0].content) ) else: # insert the system message prompt_messages.insert(0, SystemPromptMessage( content=block_prompts .replace("{{instructions}}", f"Please output a valid {code_block} with markdown codeblocks.") )) if len(prompt_messages) > 0 and isinstance(prompt_messages[-1], UserPromptMessage): # add ```JSON\n to the last message prompt_messages[-1].content += f"\n```{code_block}\n" else: # append a user message prompt_messages.append(UserPromptMessage( content=f"```{code_block}\n" )) response = self._invoke( model=model, credentials=credentials, prompt_messages=prompt_messages, model_parameters=model_parameters, tools=tools, stop=stop, stream=stream, user=user ) ``` Ensure that the `response_format` in `model_parameters` is set to "JSON" and that the stop words include `\n````, and ` ```\n` to properly handle the JSON output. This will help in stabilizing the JSON output from the Tongyi model <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/model_runtime/model_providers/tongyi/llm/llm.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#5329