How to implement streaming response when a Dify agent invokes a workflow? #8162

Closed
opened 2026-02-21 18:24:16 -05:00 by yindo · 1 comment
Owner

Originally created by @Aoaoao7 on GitHub (Feb 11, 2025).

Self Checks

  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Provide a description of requested docs changes

Image
I want my workflow conversations to return data in a stream when invoked.

Originally created by @Aoaoao7 on GitHub (Feb 11, 2025). ### Self Checks - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [x] Please do not modify this template :) and fill in all the required fields. ### Provide a description of requested docs changes ![Image](https://github.com/user-attachments/assets/ff0de23e-f019-4e83-aeff-4e3774a91444) I want my workflow conversations to return data in a stream when invoked.
yindo added the 📚 documentation label 2026-02-21 18:24:16 -05:00
yindo closed this issue 2026-02-21 18:24:16 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Feb 11, 2025):

@Aoaoao7 你好! 我是Dosu,一个可以帮助解决 bug、回答问题并帮助你成为贡献者的机器人。我会在此等待人类维护者,有什么我可以帮助你的吗?

To implement a streaming response when a Dify agent invokes a workflow, you can use the invoke method of the workflow interface with the response_mode set to "streaming". This will return a generator that yields data as it becomes available. Here is an example implementation:

import json
from typing import Mapping
from werkzeug import Request, Response
from dify_plugin import Endpoint

class Duck(Endpoint):
    def _invoke(self, r: Request, values: Mapping, settings: Mapping) -> Response:
        """
        Invoke the endpoint with the given request.
        """
        app_id = values["app_id"]
        def generator():
            response = self.session.app.workflow.invoke(
                app_id=app_id, inputs={}, response_mode="streaming", files=[]
            )
            for data in response:
                yield f"{json.dumps(data)} <br>"
        return Response(generator(), status=200, content_type="text/html")

In this example, the generator function is used to yield each piece of data from the streaming response, which is then returned as an HTTP response with a content type of text/html [1].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

@dosubot[bot] commented on GitHub (Feb 11, 2025): <!-- Greeting --> @Aoaoao7 你好! 我是[Dosu](https://dosu.dev),一个可以帮助解决 bug、回答问题并帮助你成为贡献者的机器人。我会在此等待人类维护者,有什么我可以帮助你的吗? <!-- Answer --> To implement a streaming response when a Dify agent invokes a workflow, you can use the `invoke` method of the `workflow` interface with the `response_mode` set to `"streaming"`. This will return a generator that yields data as it becomes available. Here is an example implementation: ```python import json from typing import Mapping from werkzeug import Request, Response from dify_plugin import Endpoint class Duck(Endpoint): def _invoke(self, r: Request, values: Mapping, settings: Mapping) -> Response: """ Invoke the endpoint with the given request. """ app_id = values["app_id"] def generator(): response = self.session.app.workflow.invoke( app_id=app_id, inputs={}, response_mode="streaming", files=[] ) for data in response: yield f"{json.dumps(data)} <br>" return Response(generator(), status=200, content_type="text/html") ``` In this example, the `generator` function is used to yield each piece of data from the streaming response, which is then returned as an HTTP response with a content type of `text/html` <sup>[[1]](https://github.com/langgenius/dify-docs/blob/main/jp/plugins/schema-definition/reverse-invocation-of-the-dify-service/app.md)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/1db6708f-a759-4665-b775-7af32232ec12?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/1db6708f-a759-4665-b775-7af32232ec12?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/1db6708f-a759-4665-b775-7af32232ec12?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/1db6708f-a759-4665-b775-7af32232ec12?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/1db6708f-a759-4665-b775-7af32232ec12?feedback_type=hallucination) | [Other](https://app.dosu.dev/response-feedback/1db6708f-a759-4665-b775-7af32232ec12?feedback_type=other)</sup>
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#8162