Repository navigation
fix(dify-agent): deduplicate plugin tool JSON output by content, not formatting - #45297
Merged
Merged
Conversation
_convert_tool_response_to_text compared JSON stream parts with the text parts by raw string, so a plugin returning the same object as both a text and a json message had its output appended twice whenever the two renderings differed (compact vs spaced JSON, key order, unicode escapes, arrays). The API counterpart tool_response_to_str was fixed the same way in langgenius#42196 (langgenius#42195); port the canonical-JSON comparison to the agent runtime.
wylswz
approved these changes
Nov 2, 2026
1 task done
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 #45296.
dify-agent's_convert_tool_response_to_textdeduplicated JSON stream parts against the text parts with a plain string comparison:A plugin that returns the same object both as a
textmessage and as ajsonmessage therefore had its output appended twice, because the JSON side is re-serialised withjson.dumps(...)and no longer matches the plugin's own formatting (compact vs spaced, key order, unicode escapes, whitespace, arrays). The function's docstring already promises content-level deduplication.The API counterpart (
ToolEngine.tool_response_to_str) had the same defect; it was fixed in #42196 for #42195 by canonicalising JSON on both sides before comparing. This ports that fix to the agent runtime.How
_normalized_json_part, which renders a part as canonical JSON (sort_keys=True,separators=(",", ":")) and returns the input unchanged when it is not JSON or is nested too deeply.Behaviour for non-JSON text and for genuinely different JSON is unchanged.
Verification
dify-agent/tests/local/dify_agent/layers/dify_plugin/test_layers.pystream atextframe plus ajsonframe through the tools layer and assert the observation contains the payload once: compact JSON, extra whitespace, reordered keys, nested reordered keys, unicode escapes and a JSON array. One more test asserts that a JSON part that differs from the text is still appended.'{"a":1,"b":2}{"a": 1, "b": 2}'); with ituv run python -m pytest tests/local/dify_agent/layers/dify_plugin -q→ 24 passed.ruff check/ruff format --checkclean; fulluv run pytest tests/local→ 22 failed / 1023 passed / 2 errors, the same pre-existing environment failures as before the change (e2b SDK version,openshellunavailable on Windows,python3shell scripts in server tests).AI assistance
The change was prepared with an AI coding assistant; the problem, the fix and the test results above were reviewed and reproduced locally against
main.