[Python] Inquiry: _invoke() Hangs When Executing Query with Dify Plugin SDK

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    Inquiry: _invoke() Hangs When Executing Query with Dify Plugin SDK I'm currently developing a plugin using the Dify Plugin SDK that connects to a database, executes a query, and returns the result. However, I'm experiencing a hang issue inside the _invoke() method when I attempt to execute a query and return the result. Eventually, a connection error occurs.

    Situation


    • The database connection, query execution, and result retrieval all work correctly.


    • However, when executing the query inside the _invoke() method and trying to return a result using yield, the process hangs for a long time and finally leads to a connection timeout or error.

    Code That Works as Expected

    from collections.abc import Generator
    from typing import Any

    from dify_plugin import Tool
    from dify_plugin.entities.tool import ToolInvokeMessage

    from tools.snowflake_client import SnowflakeClient

    class SnowflakeQueryTool(Tool):
    def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.sf_client = SnowflakeClient(self.runtime.credentials)

    def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:
    yield self.create_text_message("Hello, world!")


    This basic example runs successfully without any issues.

    Code That Causes the Problem

    from collections.abc import Generator
    from typing import Any

    from dify_plugin import Tool
    from dify_plugin.entities.tool import ToolInvokeMessage

    from tools.snowflake_client import SnowflakeClient

    class SnowflakeQueryTool(Tool):
    def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.sf_client = SnowflakeClient(self.runtime.credentials)

    def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:
    res = self.sf_client.execute_query("SELECT CURRENT_VERSION()")
    for row in res:
    print(row)

    yield self.create_text_message("Hello, world!")



    The print(row) outputs the expected result, which confirms that the Snowflake connection is working properly.

    However, when testing the plugin through the Dify plugin test interface, the plugin gets stuck in the "Running" state and eventually fails with a connection error.

    enter image description here

    Additional Observations


    • If I manually click Stop and then Start Run again, the plugin responds successfully.


    • Once it succeeds once, it continues to work fine without needing to click Stop, until I restart the environment.

    Assumptions & Request


    • I suspect the issue may be related to how yield and Generator are handled inside the plugin, possibly involving async/sync execution or evaluation timing.


    • I would appreciate any insight into the root cause of this behavior and how to resolve it.

    Could this be an issue within the Dify SDK itself, where the yield or database call is interfering with how responses are evaluated and streamed? enter image description here

    Any guidance or advice on how to fix or work around this issue would be greatly appreciated.

    Continue reading...

Compartilhe esta Página