Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(dataflow): preserve scalar text output shape (#18921)

huyua9db74811549a4
diff --git a/rag/svr/task_executor.py b/rag/svr/task_executor.py
index fbda9b9669..20b2790447 100644
--- a/rag/svr/task_executor.py
+++ b/rag/svr/task_executor.py
@@ -793,6 +793,19 @@ async def embedding(docs, mdl, parser_config=None, callback=None):
return tk_count, vector_size
+def _normalize_dataflow_output(chunks):
+ """Normalize pipeline output into the chunk shape used by indexing."""
+ if "chunks" in chunks:
+ return copy.deepcopy(chunks["chunks"]), "chunks"
+ if "json" in chunks:
+ return copy.deepcopy(chunks["json"]), "json"
+ for output_type in ("markdown", "text", "html"):
+ if output_type in chunks:
+ payload = chunks[output_type]
+ return ([{"text": payload}] if payload else []), output_type
+ return [], "empty"
+
+
@timed_with_recording
async def run_dataflow(task: dict):
from api.db.services.canvas_service import UserCanvasService
@@ -837,24 +850,7 @@ async def run_dataflow(task: dict):
embedding_token_consumption = chunks.get("embedding_token_consumption", 0)
# The output key may exist with an empty payload; check presence, not truthiness.
- if "chunks" in chunks:
- chunks = copy.deepcopy(chunks["chunks"])
- output_type = "chunks"
- elif "json" in chunks:
- chunks = copy.deepcopy(chunks["json"])
- output_type = "json"
- elif "markdown" in chunks:
- chunks = [{"text": [chunks["markdown"]]}] if chunks["markdown"] else []
- output_type = "markdown"
- elif "text" in chunks:
- chunks = [{"text": [chunks["text"]]}] if chunks["text"] else []
- output_type = "text"
- elif "html" in chunks:
- chunks = [{"text": [chunks["html"]]}] if chunks["html"] else []
- output_type = "html"
- else:
- chunks = []
- output_type = "empty"
+ chunks, output_type = _normalize_dataflow_output(chunks)
get_recording_context().record("pipeline_output_type", output_type)
get_recording_context().record("pipeline_output_count", len(chunks))
diff --git a/test/unit_test/rag/svr/test_task_executor_dataflow_output.py b/test/unit_test/rag/svr/test_task_executor_dataflow_output.py
new file mode 100644
index 0000000000..399c116b20
--- /dev/null
+++ b/test/unit_test/rag/svr/test_task_executor_dataflow_output.py
@@ -0,0 +1,29 @@
+import ast
+from pathlib import Path
+
+import pytest
+
+
+def normalize_dataflow_output():
+ repo_root = Path(__file__).resolve().parents[4]
+ source = (repo_root / "rag" / "svr" / "task_executor.py").read_text(encoding="utf-8")
+ tree = ast.parse(source)
+ helper = next(node for node in tree.body if isinstance(node, ast.FunctionDef) and node.name == "_normalize_dataflow_output")
+ namespace = {"copy": __import__("copy")}
+ exec(compile(ast.Module(body=[helper], type_ignores=[]), "task_executor.py", "exec"), namespace)
+ return namespace["_normalize_dataflow_output"]
+
+
[email protected]("output_type", ["markdown", "text", "html"])
+def test_scalar_dataflow_output_keeps_text_as_string(normalize_dataflow_output, output_type):
+ chunks, normalized_type = normalize_dataflow_output({output_type: "hello"})
+
+ assert normalized_type == output_type
+ assert chunks == [{"text": "hello"}]
+ assert isinstance(chunks[0]["text"], str)
+
+
[email protected]("output_type", ["markdown", "text", "html"])
+def test_empty_scalar_dataflow_output_has_no_chunks(normalize_dataflow_output, output_type):
+ assert normalize_dataflow_output({output_type: ""}) == ([], output_type)