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(agent): normalize null LogMessage metadata in Agent V2 tool client (#39599)

David Park3f2eeec1e0c6
diff --git a/dify-agent/src/dify_agent/layers/dify_plugin/tool_client.py b/dify-agent/src/dify_agent/layers/dify_plugin/tool_client.py
index cff1ca78c29..26c27d35a44 100644
--- a/dify-agent/src/dify_agent/layers/dify_plugin/tool_client.py
+++ b/dify-agent/src/dify_agent/layers/dify_plugin/tool_client.py
@@ -96,6 +96,15 @@ class DifyPluginToolInvokeMessage(BaseModel):
data: Mapping[str, object] = Field(default_factory=dict)
metadata: Mapping[str, object] = Field(default_factory=dict)
+ @field_validator("metadata", mode="before")
+ @classmethod
+ def _normalize_metadata(cls, value: object) -> object:
+ # The plugin SDK's ``create_log_message`` defaults ``metadata`` to
+ # None, which the daemon serializes as ``metadata: null``. Mirror the
+ # canonical core model (ToolInvokeMessage.LogMessage) and coerce that
+ # null back to an empty mapping so the message union still resolves.
+ return value or {}
+
class MessageType(StrEnum):
TEXT = "text"
IMAGE = "image"
diff --git a/dify-agent/tests/local/dify_agent/layers/dify_plugin/test_tool_client.py b/dify-agent/tests/local/dify_agent/layers/dify_plugin/test_tool_client.py
index 60471e00dac..2ca04a228b9 100644
--- a/dify-agent/tests/local/dify_agent/layers/dify_plugin/test_tool_client.py
+++ b/dify-agent/tests/local/dify_agent/layers/dify_plugin/test_tool_client.py
@@ -1,4 +1,20 @@
-"""Blob chunk merging validates the plugin-declared size before allocating."""
+"""Regression tests for tool-stream message validation and blob chunk merging.
+
+Agent V2 talks to the plugin daemon through its own copy of the tool-stream
+message model. A tool that emits an operation log via the plugin SDK's
+``create_log_message`` leaves ``metadata`` unset, and the daemon serializes it
+onto the wire as ``metadata: null``. The canonical core model
+(``core.tools.entities.tool_entities.ToolInvokeMessage.LogMessage``) normalizes
+that null to ``{}``; Agent V2's copy must do the same, otherwise every such log
+message collapses the whole ``message`` union and the tool invocation fails with
+``N validation errors for DifyPluginToolInvokeMessage``.
+
+See langgenius/dify-official-plugins#3512.
+
+Blob chunk merging validates the plugin-declared size before allocating.
+"""
+
+from __future__ import annotations
import pytest
@@ -8,6 +24,48 @@ from dify_agent.layers.dify_plugin.tool_client import (
)
+def test_log_message_with_null_metadata_normalizes_to_empty_dict() -> None:
+ """A LogMessage whose ``metadata`` is null must validate, not raise.
+
+ This is the exact wire shape produced when a plugin tool calls
+ ``create_log_message(label=..., data=..., status=...)`` without passing
+ ``metadata`` (the SDK default), e.g. the Google Calendar tool.
+ """
+ message = DifyPluginToolInvokeMessage.model_validate(
+ {
+ "type": "log",
+ "message": {
+ "id": "log-1",
+ "label": "Search Events Operation",
+ "status": "success",
+ "data": {"calendar_id": "primary"},
+ "metadata": None,
+ },
+ }
+ )
+
+ assert isinstance(message.message, DifyPluginToolInvokeMessage.LogMessage)
+ assert message.message.metadata == {}
+
+
+def test_log_message_with_omitted_metadata_still_validates() -> None:
+ """Omitting ``metadata`` entirely must also validate to an empty dict."""
+ message = DifyPluginToolInvokeMessage.model_validate(
+ {
+ "type": "log",
+ "message": {
+ "id": "log-2",
+ "label": "Fetching Top Stories",
+ "status": "success",
+ "data": {"limit": 10},
+ },
+ }
+ )
+
+ assert isinstance(message.message, DifyPluginToolInvokeMessage.LogMessage)
+ assert message.message.metadata == {}
+
+
def blob_chunk(
*, chunk_id: str, sequence: int, total_length: int, blob: bytes, end: bool
) -> DifyPluginToolInvokeMessage: