From 2655425fbe57fdd788e7490a07c68bcabac30842 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Wed, 11 Mar 2026 14:45:55 +0800 Subject: [PATCH] fix: deduplicate final chunk yield in Dify chatflow streaming (#2049) * Initial plan * fix: prevent duplicate messages when Dify chatflow sends both workflow_finished and message_end events Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com> * style: apply ruff formatting to difysvapi.py Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com> --- src/langbot/pkg/provider/runners/difysvapi.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/langbot/pkg/provider/runners/difysvapi.py b/src/langbot/pkg/provider/runners/difysvapi.py index 98da78c1..039bf33a 100644 --- a/src/langbot/pkg/provider/runners/difysvapi.py +++ b/src/langbot/pkg/provider/runners/difysvapi.py @@ -441,6 +441,7 @@ class DifyServiceAPIRunner(runner.RequestRunner): is_final = False think_start = False think_end = False + yielded_final = False remove_think = self.pipeline_config['output'].get('misc', '').get('remove-think') @@ -493,13 +494,19 @@ class DifyServiceAPIRunner(runner.RequestRunner): if answer: basic_mode_pending_chunk = answer - if (is_final or message_idx % 8 == 0) and (basic_mode_pending_chunk != '' or is_final): + if ( + not yielded_final + and (is_final or message_idx % 8 == 0) + and (basic_mode_pending_chunk != '' or is_final) + ): # content, _ = self._process_thinking_content(basic_mode_pending_chunk) yield provider_message.MessageChunk( role='assistant', content=basic_mode_pending_chunk, is_final=is_final, ) + if is_final: + yielded_final = True if chunk is None: raise errors.DifyAPIError('Dify API 没有返回任何响应,请检查网络连接和API配置')