Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

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

Aling33cbea438816f3
diff --git a/dify-agent/src/dify_agent/layers/dify_plugin/tools_layer.py b/dify-agent/src/dify_agent/layers/dify_plugin/tools_layer.py
index 3d27ffbde8f..f6e12667cd7 100644
--- a/dify-agent/src/dify_agent/layers/dify_plugin/tools_layer.py
+++ b/dify-agent/src/dify_agent/layers/dify_plugin/tools_layer.py
@@ -598,6 +598,14 @@ def _tool_error_text(*, tool_name: str, error: DifyPluginToolClientError) -> str
return f"tool invoke error: {error}"
+def _normalized_json_part(text: str) -> str:
+ """Render one part as canonical JSON so formatting differences do not hide duplicates."""
+ try:
+ return json.dumps(json.loads(text), ensure_ascii=False, sort_keys=True, separators=(",", ":"))
+ except (ValueError, RecursionError):
+ return text
+
+
def _convert_tool_response_to_text(tool_response: Sequence[DifyPluginToolInvokeMessage]) -> str:
"""Convert daemon stream messages into the plain-text tool observation.
@@ -638,8 +646,8 @@ def _convert_tool_response_to_text(tool_response: Sequence[DifyPluginToolInvokeM
parts.append(str(response.message))
if json_parts:
- existing_parts = set(parts)
- parts.extend(part for part in json_parts if part not in existing_parts)
+ existing_parts = {_normalized_json_part(part) for part in parts}
+ parts.extend(part for part in json_parts if _normalized_json_part(part) not in existing_parts)
return "".join(parts)
diff --git a/dify-agent/tests/local/dify_agent/layers/dify_plugin/test_layers.py b/dify-agent/tests/local/dify_agent/layers/dify_plugin/test_layers.py
index 717e8af4cf0..e5d367441f5 100644
--- a/dify-agent/tests/local/dify_agent/layers/dify_plugin/test_layers.py
+++ b/dify-agent/tests/local/dify_agent/layers/dify_plugin/test_layers.py
@@ -205,3 +205,81 @@ async def test_model_assembly_reads_config_reference_and_borrows_shared_client()
assert model.model_name == "demo"
assert model.provider.client.http_client is client
assert model.provider.name == "DifyAPI/langgenius/openai"
+
+
+def _single_tool_config() -> tools_layer.Config:
+ return tools_layer.Config(
+ tools=[
+ {
+ "plugin_id": "langgenius/swagger",
+ "provider": "swagger",
+ "tool_name": "get_item",
+ "name": "get_item",
+ "credential_type": "unauthorized",
+ }
+ ]
+ )
+
+
+def _daemon_stream(*frames: dict) -> str:
+ return "".join("data: " + json.dumps(frame) + "\n" for frame in frames)
+
+
+def _text_frame(text: str) -> dict:
+ return {"code": 0, "message": "ok", "data": {"type": "text", "message": {"text": text}}}
+
+
+def _json_frame(json_object: dict | list) -> dict:
+ return {"code": 0, "message": "ok", "data": {"type": "json", "message": {"json_object": json_object}}}
+
+
+ ("text", "json_object"),
+ [
+ ('{"a":1,"b":2}', {"a": 1, "b": 2}),
+ (' \n{ "a": 1, "b": 2 }\t', {"a": 1, "b": 2}),
+ ('{"b": 2, "a": 1}', {"a": 1, "b": 2}),
+ ('{"items":[{"b":2,"a":1}]}', {"items": [{"a": 1, "b": 2}]}),
+ (r'{"\u540d\u79f0":"\u4e2d\u6587"}', {"名称": "中文"}),
+ ('[{"b":2,"a":1},null]', [{"a": 1, "b": 2}, None]),
+ ],
+)
+async def test_native_plugin_tool_deduplicates_json_output_that_arrived_as_equivalent_text(text, json_object):
+ def handler(request):
+ return httpx.Response(200, text=_daemon_stream(_text_frame(text), _json_frame(json_object)))
+
+ async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
+ deps = Deps(
+ layers={
+ "tools": {"config": _single_tool_config().model_dump(mode="json"), "state": {}},
+ "execution_context": {"config": identity().model_dump(mode="json"), "state": {}},
+ },
+ services=Services(client, client, plugin_daemon_api_key="daemon-secret"),
+ run_id="test",
+ )
+ result = await invoke_native_tool(deps, tools_layer.Toolset("tools"), "get_item", {})
+
+ assert result.output == text
+
+
+async def test_native_plugin_tool_keeps_json_output_that_differs_from_the_text():
+ def handler(request):
+ return httpx.Response(
+ 200,
+ text=_daemon_stream(_text_frame("summary"), _json_frame({"a": 1})),
+ )
+
+ async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
+ deps = Deps(
+ layers={
+ "tools": {"config": _single_tool_config().model_dump(mode="json"), "state": {}},
+ "execution_context": {"config": identity().model_dump(mode="json"), "state": {}},
+ },
+ services=Services(client, client, plugin_daemon_api_key="daemon-secret"),
+ run_id="test",
+ )
+ result = await invoke_native_tool(deps, tools_layer.Toolset("tools"), "get_item", {})
+
+ assert result.output == 'summary{"a": 1}'