Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix: use MESSAGE event type for LLM text chunks (#38302)

felixca01a69f87ba
diff --git a/api/core/app/task_pipeline/easy_ui_based_generate_task_pipeline.py b/api/core/app/task_pipeline/easy_ui_based_generate_task_pipeline.py
index ad27ea51eb8..eaf5642f0f6 100644
--- a/api/core/app/task_pipeline/easy_ui_based_generate_task_pipeline.py
+++ b/api/core/app/task_pipeline/easy_ui_based_generate_task_pipeline.py
@@ -74,7 +74,6 @@ class EasyUIBasedGenerateTaskPipeline(BasedGenerateTaskPipeline[EasyUIAppGenerat
"""
_task_state: EasyUITaskState
- _precomputed_event_type: StreamEvent | None = None
def __init__(
self,
@@ -372,15 +371,10 @@ class EasyUIBasedGenerateTaskPipeline(BasedGenerateTaskPipeline[EasyUIAppGenerat
current_content += delta_text
self._task_state.llm_result.message.content = current_content
- # Determine the event type once, on first LLM chunk, and reuse for subsequent chunks
- if not hasattr(self, "_precomputed_event_type") or self._precomputed_event_type is None:
- self._precomputed_event_type = self._message_cycle_manager.get_message_event_type(
- message_id=self._message_id
- )
yield self._message_cycle_manager.message_to_stream_response(
answer=delta_text,
message_id=self._message_id,
- event_type=self._precomputed_event_type,
+ event_type=StreamEvent.MESSAGE,
)
case QueueMessageReplaceEvent():
yield self._message_cycle_manager.message_replace_to_stream_response(answer=event.text)
diff --git a/api/tests/unit_tests/core/app/task_pipeline/test_easy_ui_based_generate_task_pipeline.py b/api/tests/unit_tests/core/app/task_pipeline/test_easy_ui_based_generate_task_pipeline.py
index 3f5c8bc1295..76a7cde0f43 100644
--- a/api/tests/unit_tests/core/app/task_pipeline/test_easy_ui_based_generate_task_pipeline.py
+++ b/api/tests/unit_tests/core/app/task_pipeline/test_easy_ui_based_generate_task_pipeline.py
@@ -246,10 +246,8 @@ class TestEasyUiBasedGenerateTaskPipelineCases:
pipeline._task_state = task_state
return pipeline
- def test_get_message_event_type_called_once_when_first_llm_chunk_arrives(
- self, pipeline, mock_message_cycle_manager
- ):
- """Expect get_message_event_type to be called when processing the first LLM chunk event."""
+ def test_get_message_event_type_not_called_for_llm_text_chunks(self, pipeline, mock_message_cycle_manager):
+ """Expect LLM text chunks to always use MESSAGE event type."""
# Setup a minimal LLM chunk event
chunk = llm_chunk("hi")
llm_chunk_event = QueueLLMChunkEvent(chunk=chunk)
@@ -260,7 +258,10 @@ class TestEasyUiBasedGenerateTaskPipelineCases:
list(pipeline._process_stream_response(publisher=None))
# Assert
- mock_message_cycle_manager.get_message_event_type.assert_called_once_with(message_id="test-message-id")
+ mock_message_cycle_manager.get_message_event_type.assert_not_called()
+ mock_message_cycle_manager.message_to_stream_response.assert_called_once_with(
+ answer="hi", message_id="test-message-id", event_type=StreamEvent.MESSAGE
+ )
def test_llm_chunk_event_with_text_content(self, pipeline, mock_message_cycle_manager, task_state):
"""Test handling of LLM chunk events with text content."""