Use requests in plug-in development #28

Closed
opened 2026-02-22 17:16:17 -05:00 by yindo · 4 comments
Owner

Originally created by @foogle945 on GitHub (Apr 16, 2025).

I'm encountering an issue while developing a tool plugin. My code is as follows:

from collections.abc import Generator
from typing import Any
from dify_plugin import Tool
from dify_plugin.entities.tool import ToolInvokeMessage
import requests

class MyTestTool(Tool):
    def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage,None,None]:
        # response = requests.get("https://baidu.com")
        # response.raise_for_status()
        yield self.create_text_message(
            "success"
        )

When I keep the response = requests.get("https://baidu.com") line commented out, the plugin works normally and returns successfully. However, when I uncomment and include this line, the plugin keeps running and eventually throws the following exception:

Failed to read data from 10.50.101.153:5003
Traceback (most recent call last):
  File "C:\Users\12525\AppData\Local\Programs\Python\Python313\Lib\site-packages\dify_plugin\core\server\tcp\request_reader.py", line 168, in _read_stream
    raise Exception("Connection is closed")
Exception: Connection is closed
Unexpected error occurred when executing request
Traceback (most recent call last):
  File "C:\Users\12525\AppData\Local\Programs\Python\Python313\Lib\site-packages\dify_plugin\core\server\io_server.py", line 92, in _execute_request_in_thread
    self._execute_request(
    ~~~~~~~~~~~~~~~~~~~~~^
        session_id,
        ^^^^^^^^^^^
    ...<6 lines>...
        endpoint_id,
        ^^^^^^^^^^^^
    )
    ^
  File "C:\Users\12525\AppData\Local\Programs\Python\Python313\Lib\site-packages\dify_plugin\plugin.py", line 379, in _execute_request
    writer.session_message(
    ~~~~~~~~~~~~~~~~~~~~~~^
        session_id=session_id,
        ^^^^^^^^^^^^^^^^^^^^^^
        data=writer.stream_object(data=message),
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\12525\AppData\Local\Programs\Python\Python313\Lib\site-packages\dify_plugin\core\server\__base\response_writer.py", line 52, in session_message
    return self.put(Event.SESSION, session_id, data)
           ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\12525\AppData\Local\Programs\Python\Python313\Lib\site-packages\dify_plugin\core\server\__base\response_writer.py", line 40, in put
    self.write("\n\n")
    ~~~~~~~~~~^^^^^^^^
  File "C:\Users\12525\AppData\Local\Programs\Python\Python313\Lib\site-packages\dify_plugin\core\server\tcp\request_reader.py", line 82, in write
    raise Exception("connection is dead")
Exception: connection is dead

What could be causing this issue? How can I resolve it?

Originally created by @foogle945 on GitHub (Apr 16, 2025). I'm encountering an issue while developing a tool plugin. My code is as follows: ``` from collections.abc import Generator from typing import Any from dify_plugin import Tool from dify_plugin.entities.tool import ToolInvokeMessage import requests class MyTestTool(Tool): def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage,None,None]: # response = requests.get("https://baidu.com") # response.raise_for_status() yield self.create_text_message( "success" ) ``` When I keep the **response = requests.get("https://baidu.com")** line commented out, the plugin works normally and returns successfully. However, when I uncomment and include this line, the plugin keeps running and eventually throws the following exception: ``` Failed to read data from 10.50.101.153:5003 Traceback (most recent call last): File "C:\Users\12525\AppData\Local\Programs\Python\Python313\Lib\site-packages\dify_plugin\core\server\tcp\request_reader.py", line 168, in _read_stream raise Exception("Connection is closed") Exception: Connection is closed Unexpected error occurred when executing request Traceback (most recent call last): File "C:\Users\12525\AppData\Local\Programs\Python\Python313\Lib\site-packages\dify_plugin\core\server\io_server.py", line 92, in _execute_request_in_thread self._execute_request( ~~~~~~~~~~~~~~~~~~~~~^ session_id, ^^^^^^^^^^^ ...<6 lines>... endpoint_id, ^^^^^^^^^^^^ ) ^ File "C:\Users\12525\AppData\Local\Programs\Python\Python313\Lib\site-packages\dify_plugin\plugin.py", line 379, in _execute_request writer.session_message( ~~~~~~~~~~~~~~~~~~~~~~^ session_id=session_id, ^^^^^^^^^^^^^^^^^^^^^^ data=writer.stream_object(data=message), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "C:\Users\12525\AppData\Local\Programs\Python\Python313\Lib\site-packages\dify_plugin\core\server\__base\response_writer.py", line 52, in session_message return self.put(Event.SESSION, session_id, data) ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\12525\AppData\Local\Programs\Python\Python313\Lib\site-packages\dify_plugin\core\server\__base\response_writer.py", line 40, in put self.write("\n\n") ~~~~~~~~~~^^^^^^^^ File "C:\Users\12525\AppData\Local\Programs\Python\Python313\Lib\site-packages\dify_plugin\core\server\tcp\request_reader.py", line 82, in write raise Exception("connection is dead") Exception: connection is dead ``` What could be causing this issue? How can I resolve it?
yindo closed this issue 2026-02-22 17:16:17 -05:00
Author
Owner

@yizhi2632438 commented on GitHub (Apr 29, 2025):

me too

@yizhi2632438 commented on GitHub (Apr 29, 2025): me too
Author
Owner

@wangpengfei44 commented on GitHub (Jun 30, 2025):

me too

@wangpengfei44 commented on GitHub (Jun 30, 2025): me too
Author
Owner

@fernvenue commented on GitHub (Jun 30, 2025):

Hi @wangpengfei44, I think this may related to https://github.com/langgenius/dify/issues/17567#issuecomment-2829296013 this, could you please give it a try?

@fernvenue commented on GitHub (Jun 30, 2025): Hi @wangpengfei44, I think this may related to https://github.com/langgenius/dify/issues/17567#issuecomment-2829296013 this, could you please give it a try?
Author
Owner

@crazywoola commented on GitHub (Feb 3, 2026):

Hi @foogle945, thanks for opening this issue.

Why this is being closed

We could not extract an actionable task from the current report.

  • Description contains placeholders instead of concrete details.

Next steps

Please open a new issue that includes:

  • A clear problem statement
  • Reproducible steps or concrete scope
  • Expected result
  • Actual result and logs/screenshots when available

Thanks for understanding and for helping keep the issue tracker actionable.

@crazywoola commented on GitHub (Feb 3, 2026): Hi @foogle945, thanks for opening this issue. ### Why this is being closed We could not extract an actionable task from the current report. - Description contains placeholders instead of concrete details. ### Next steps Please open a new issue that includes: - A clear problem statement - Reproducible steps or concrete scope - Expected result - Actual result and logs/screenshots when available Thanks for understanding and for helping keep the issue tracker actionable.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-plugins#28