Skip to content

fix(dify-agent): deduplicate plugin tool JSON output by content, not formatting - #45297

Merged
wylswz merged 1 commit into
langgenius:mainfrom
Aling33:fix/dify-agent-tool-json-dedupe
Nov 2, 2026
Merged

wylswz merged 1 commit into
langgenius:mainfrom
Aling33:fix/dify-agent-tool-json-dedupe

Conversation

@Aling33

@Aling33 Aling33 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #45296.

dify-agent's _convert_tool_response_to_text deduplicated JSON stream parts against the text parts with a plain string comparison:

existing_parts = set(parts)
parts.extend(part for part in json_parts if part not in existing_parts)

A plugin that returns the same object both as a text message and as a json message therefore had its output appended twice, because the JSON side is re-serialised with json.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

  • Added _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.
  • Both the existing text parts and the incoming JSON parts are compared in that canonical form.

Behaviour for non-JSON text and for genuinely different JSON is unchanged.

Verification

  • New tests in dify-agent/tests/local/dify_agent/layers/dify_plugin/test_layers.py stream a text frame plus a json frame 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.
  • Without the fix those six parametrised cases fail ('{"a":1,"b":2}{"a": 1, "b": 2}'); with it uv run python -m pytest tests/local/dify_agent/layers/dify_plugin -q → 24 passed.
  • ruff check / ruff format --check clean; full uv run pytest tests/local → 22 failed / 1023 passed / 2 errors, the same pre-existing environment failures as before the change (e2b SDK version, openshell unavailable on Windows, python3 shell 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.

_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.
@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 2, 2026
@wylswz
wylswz added this pull request to the merge queue Nov 2, 2026
Merged via the queue into langgenius:main with commit cbea438 Nov 2, 2026
38 checks passed
@laipz8200 laipz8200 mentioned this pull request Nov 3, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] dify-agent: plugin tool observations repeat JSON output that already arrived as text

2 participants