Skip to content

normalize json before deduplicating in ToolEngine.tool_response_to_str #42195

Closed

Description

@wylswz

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • 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, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.17.1

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

  1. connect to a swagger mcp that returns compact json object
  2. let agent to call that tool
  3. check tool call history, finding two duplicated objects, a compact one and another formatted one

✔️ Expected Behavior

It should be deduplicated as documented in the function.

❌ Actual Behavior

Objects are duplicated.

Image

Activity

  1. added
    🐞 bug
    on Sep 11, 2026
  2. self-assigned this
    on Sep 11, 2026
  3. dosubot commented on Sep 11, 2026

    @dosubot

    Confirmed — the dedup in ToolEngine.tool_response_to_str compares raw strings rather than parsed structures [1]:

    for response in tool_response:
        ...
        elif response.type == ToolInvokeMessage.MessageType.JSON:
            json_message = cast(ToolInvokeMessage.JsonMessage, response.message)
            if json_message.suppress_output:
                continue
            json_parts.append(
                json.dumps(safe_json_value(json_message.json_object), ensure_ascii=False)
            )
        ...
    
    # Add JSON parts, avoiding duplicates from text parts.
    if json_parts:
        existing_parts = set(parts)
        parts.extend(p for p in json_parts if p not in existing_parts)

    existing_parts is built from the TEXT/LINK/etc. messages as raw strings, and each entry in json_parts is checked against that set with plain string equality. For MCP tools, MCPTool._process_text_content parses the tool's raw text output and re-emits it as a JSON message when it looks like a JSON object or array [2]. So the same data can appear twice: the tool's original compact JSON as text, and the reformatted json.dumps(...) output from the JSON message. Different whitespace or key order makes the two strings byte-for-byte distinct, the set membership check misses, and both land in the observation.

    Prior duplicate-output fixes in this function [3] [4] [5] covered cases like VARIABLE messages and workflow-as-tool JSON/text pairs, but all of them compare raw strings too. Catching compact vs. pretty-printed JSON requires canonicalizing first — json.loads then re-dumps with sorted keys, or comparing the parsed objects directly — which the current implementation doesn't do.

    Want me to sketch a fix that parses JSON-looking text parts before building existing_parts?

    To reply, just mention @dosu.


    Docs are dead. Just use Dosu.

    Leave Feedback Ask Dosu about dify

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

🐞 bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions