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(agent): flush trailing incomplete action/thought prefixes in ReAct stream parser (#43353)

Felix Shi6f82525a72b0
diff --git a/api/core/agent/output_parser/cot_output_parser.py b/api/core/agent/output_parser/cot_output_parser.py
index 75da9133b5f..67e96da4f85 100644
--- a/api/core/agent/output_parser/cot_output_parser.py
+++ b/api/core/agent/output_parser/cot_output_parser.py
@@ -218,3 +218,11 @@ class CotAgentOutputParser:
if json_cache:
yield parse_action(json_cache)
+
+ # Flush a trailing incomplete "action:"/"thought:" prefix so ordinary
+ # text at the end of the stream is not silently dropped (issue #43350).
+ if action_cache:
+ yield action_cache
+
+ if thought_cache:
+ yield thought_cache
diff --git a/api/tests/unit_tests/core/agent/output_parser/test_cot_output_parser.py b/api/tests/unit_tests/core/agent/output_parser/test_cot_output_parser.py
index 0497456a16d..cfb8a3ec225 100644
--- a/api/tests/unit_tests/core/agent/output_parser/test_cot_output_parser.py
+++ b/api/tests/unit_tests/core/agent/output_parser/test_cot_output_parser.py
@@ -195,6 +195,43 @@ class TestCotAgentOutputParser:
result = list(CotAgentOutputParser.handle_react_stream_output(chunks, usage_dict))
assert "x" in "".join(map(str, result))
+ # --------------------------------------------------------
+ # Trailing incomplete marker prefixes (issue #43350)
+ # --------------------------------------------------------
+
+ @pytest.mark.parametrize("marker", ["action:", "thought:", "ACTION:", "THOUGHT:"])
+ @pytest.mark.parametrize("prefix_len", range(1, 7))
+ def test_trailing_incomplete_prefix_is_flushed(self, make_chunk, usage_dict, marker, prefix_len) -> None:
+ text = f"hello {marker[:prefix_len]}"
+ chunks = [make_chunk(text)]
+ result = list(CotAgentOutputParser.handle_react_stream_output(chunks, usage_dict))
+ assert "".join(result) == text
+
+ @pytest.mark.parametrize("marker", ["action:", "thought:"])
+ def test_trailing_incomplete_prefix_is_flushed_char_by_char(self, make_chunk, usage_dict, marker) -> None:
+ for prefix_len in range(1, len(marker)):
+ text = f"hello {marker[:prefix_len]}"
+ chunks = [make_chunk(char) for char in text]
+ result = list(CotAgentOutputParser.handle_react_stream_output(chunks, usage_dict))
+ assert "".join(result) == text
+
+ def test_trailing_incomplete_prefix_after_complete_marker(self, make_chunk, usage_dict) -> None:
+ text = "thought: ok\nhello acti"
+ chunks = [make_chunk(text)]
+ result = list(CotAgentOutputParser.handle_react_stream_output(chunks, usage_dict))
+ joined = "".join(result)
+ assert "ok" in joined
+ assert "hello acti" in joined
+
+ @pytest.mark.parametrize("marker", ["action:", "thought:", "ACTION:", "THOUGHT:"])
+ def test_complete_trailing_marker_still_consumed(self, make_chunk, usage_dict, marker) -> None:
+ chunks = [make_chunk(f"hello {marker}")]
+ result = list(CotAgentOutputParser.handle_react_stream_output(chunks, usage_dict))
+ joined = "".join(result).lower()
+ assert "hello" in joined
+ assert "action:" not in joined
+ assert "thought:" not in joined
+
# --------------------------------------------------------
# Mixed streaming scenarios
# --------------------------------------------------------