[PR #27953] Refactor WorkflowTool to support streaming events and improve output handling #31904

Open
opened 2026-02-21 20:50:22 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/27953

State: open
Merged: No


  • Updated the invoke method to enable streaming of workflow events.
  • Introduced _stream_workflow_events method to convert streaming events into ToolInvokeMessages.
  • Added _emit_outputs_from_data method to handle final outputs and errors more effectively.
  • Enhanced unit tests to cover streaming events and error handling scenarios.

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

fix #27940

When a workflow is used as a tool, its execution result is currently returned only after completion.
This blocking behavior introduces several issues:

  • No real-time feedback:
    Long-running workflows (e.g., with LLM or Agent nodes) cannot stream incremental outputs.

  • Inconsistent behavior:
    While WorkflowAppGenerator supports SSE streaming, WorkflowTool could not consume these events.

  • Inefficient and hard to debug:
    Errors and intermediate states are hidden until the end of execution, making both user experience and debugging poor.

This PR refactors the WorkflowTool class to enable true streaming support and improve output handling:

  • Streaming Event Pipeline
    Added _stream_workflow_events() to convert workflow SSE events into ToolInvokeMessage objects, enabling real-time streaming in tool execution.

  • Unified Output Handling
    Added _emit_outputs_from_data() to normalize how final outputs, errors, and files are emitted after a workflow completes.

  • Automatic Streaming Detection
    Added _workflow_supports_streaming() to scan the workflow graph and enable streaming only when relevant node types (ANSWER, LLM, AGENT) exist.

  • Improved Tests
    Enhanced unit tests to validate streaming events, error propagation, and fallback behavior when streaming is not supported.

Screenshots

Before After
... ...

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/27953 **State:** open **Merged:** No --- - Updated the `invoke` method to enable streaming of workflow events. - Introduced `_stream_workflow_events` method to convert streaming events into ToolInvokeMessages. - Added `_emit_outputs_from_data` method to handle final outputs and errors more effectively. - Enhanced unit tests to cover streaming events and error handling scenarios. > [!IMPORTANT] > > 1. Make sure you have read our [contribution guidelines](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) > 1. Ensure there is an associated issue and you have been assigned to it > 1. Use the correct syntax to link this PR: `Fixes #<issue number>`. ## Summary fix #27940 When a workflow is used as a tool, its execution result is currently returned only after completion. This blocking behavior introduces several issues: - No real-time feedback: Long-running workflows (e.g., with LLM or Agent nodes) cannot stream incremental outputs. - Inconsistent behavior: While WorkflowAppGenerator supports SSE streaming, WorkflowTool could not consume these events. - Inefficient and hard to debug: Errors and intermediate states are hidden until the end of execution, making both user experience and debugging poor. This PR refactors the WorkflowTool class to enable true streaming support and improve output handling: - Streaming Event Pipeline Added _stream_workflow_events() to convert workflow SSE events into ToolInvokeMessage objects, enabling real-time streaming in tool execution. - Unified Output Handling Added _emit_outputs_from_data() to normalize how final outputs, errors, and files are emitted after a workflow completes. - Automatic Streaming Detection Added _workflow_supports_streaming() to scan the workflow graph and enable streaming only when relevant node types (ANSWER, LLM, AGENT) exist. - Improved Tests Enhanced unit tests to validate streaming events, error propagation, and fallback behavior when streaming is not supported. <!-- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. --> ## Screenshots | Before | After | |--------|-------| | ... | ... | ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:50:22 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#31904