पाठ 21 / 25

Streaming Responses

Show progress as the workflow runs.

Server-sent events

With response_mode set to streaming, the API returns server-sent events: lines beginning with data: that each contain a JSON event, such as workflow started, node started and finished, text chunks, and workflow finished with status and timings. Stream text chunks to the user for a faster-feeling experience, and use node events for progress indicators and diagnostics. Handle error events and dropped connections, and do not assume an answer is complete until the finished event arrives. Dify's node names, menus and options change between versions; check the current Dify documentation.

Parsing a recorded event stream, run

I ran this with plain Python 3 (scikit-learn 1.9.1 where imported). It models or tests one piece of a Dify app locally; Dify itself was not running. A recorded stream with five events is parsed line by line: two text chunks join into the answer "Open Settings, then Security.", the knowledge retrieval node took 0.41 seconds, and the final status is succeeded. The event shapes are simplified examples.

import json
# A recorded streaming response: server-sent events, one JSON object per "data:" line
stream = """data: {"event": "workflow_started", "workflow_run_id": "run-9"}

data: {"event": "node_finished", "data": {"title": "Knowledge Retrieval", "elapsed_time": 0.41}}

data: {"event": "text_chunk", "data": {"text": "Open Settings, "}}

data: {"event": "text_chunk", "data": {"text": "then Security."}}

data: {"event": "workflow_finished", "data": {"status": "succeeded", "elapsed_time": 2.3}}
"""
answer, timings = [], {}
for line in stream.splitlines():
    if not line.startswith("data: "):
        continue
    ev = json.loads(line[len("data: "):])
    if ev["event"] == "text_chunk":
        answer.append(ev["data"]["text"])
    elif ev["event"] == "node_finished":
        timings[ev["data"]["title"]] = ev["data"]["elapsed_time"]
    elif ev["event"] == "workflow_finished":
        status = ev["data"]["status"]
print("answer:", "".join(answer))
print("node timings:", timings, "| status:", status)

Output:

answer: Open Settings, then Security.
node timings: {'Knowledge Retrieval': 0.41} | status: succeeded

Use node timings

Log per-node elapsed times from stream events to find the slowest step in production.

त्वरित जाँच: When is a streamed answer complete?

  • After the first text chunk
  • When the workflow-finished event arrives
  • Immediately after the request is sent
  • When the browser tab closes
Answer

When the workflow-finished event arrives — Wait for the completion event.