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: keep Notion table cell rich text segments in one Markdown column (#41993)

Harsh Kashyap455951056a3d
diff --git a/api/core/rag/extractor/notion_extractor.py b/api/core/rag/extractor/notion_extractor.py
index 0fc57c830fb..99f2362fe68 100644
--- a/api/core/rag/extractor/notion_extractor.py
+++ b/api/core/rag/extractor/notion_extractor.py
@@ -290,6 +290,13 @@ class NotionExtractor(BaseExtractor):
result_lines = "\n".join(result_lines_arr)
return result_lines
+ @staticmethod
+ def _get_cell_text(cell: list[dict[str, Any]]) -> str:
+ # A cell is an array of rich text segments (text, mention, equation);
+ # join them so one cell always maps to one Markdown column, keeping
+ # empty cells as empty columns so the column count stays stable.
+ return "".join(segment.get("plain_text") or segment.get("text", {}).get("content", "") for segment in cell)
+
def _read_table_rows(self, block_id: str) -> str:
"""Read table rows."""
assert self._notion_access_token is not None, "Notion access token is required"
@@ -316,12 +323,7 @@ class NotionExtractor(BaseExtractor):
table_header_cell_texts = []
table_header_cells = data["results"][0]["table_row"]["cells"]
for table_header_cell in table_header_cells:
- if table_header_cell:
- for table_header_cell_text in table_header_cell:
- text = table_header_cell_text["text"]["content"]
- table_header_cell_texts.append(text)
- else:
- table_header_cell_texts.append("")
+ table_header_cell_texts.append(self._get_cell_text(table_header_cell))
# Initialize Markdown table with headers
markdown_table = "| " + " | ".join(table_header_cell_texts) + " |\n"
markdown_table += "| " + " | ".join(["---"] * len(table_header_cell_texts)) + " |\n"
@@ -331,11 +333,8 @@ class NotionExtractor(BaseExtractor):
for i in range(len(results) - 1):
column_texts = []
table_column_cells = data["results"][i + 1]["table_row"]["cells"]
- for j in range(len(table_column_cells)):
- if table_column_cells[j]:
- for table_column_cell_text in table_column_cells[j]:
- column_text = table_column_cell_text["text"]["content"]
- column_texts.append(column_text)
+ for table_column_cell in table_column_cells:
+ column_texts.append(self._get_cell_text(table_column_cell))
# Add row to Markdown table
markdown_table += "| " + " | ".join(column_texts) + " |\n"
result_lines_arr.append(markdown_table)
diff --git a/api/tests/unit_tests/core/rag/extractor/test_notion_extractor.py b/api/tests/unit_tests/core/rag/extractor/test_notion_extractor.py
index 1f313659fd8..4527bf2a4e2 100644
--- a/api/tests/unit_tests/core/rag/extractor/test_notion_extractor.py
+++ b/api/tests/unit_tests/core/rag/extractor/test_notion_extractor.py
@@ -442,6 +442,89 @@ class TestNotionBlocks:
assert "| H1 | |" in markdown
assert "| R2C1 | R2C2 |" in markdown
+ def test_read_table_rows_joins_rich_text_segments_within_cell(self, mocker: MockerFixture):
+ extractor = notion_extractor.NotionExtractor(
+ notion_workspace_id="ws",
+ notion_obj_id="obj",
+ notion_page_type="page",
+ tenant_id="tenant",
+ notion_access_token="token",
+ )
+
+ # A cell with mixed formatting arrives as multiple rich text segments.
+ page = {
+ "results": [
+ {
+ "table_row": {
+ "cells": [
+ [{"text": {"content": "Name"}}],
+ [{"text": {"content": "Desc"}}],
+ ]
+ }
+ },
+ {
+ "table_row": {
+ "cells": [
+ [{"text": {"content": "item1"}}],
+ [{"text": {"content": "plain "}}, {"text": {"content": "bold"}}],
+ ]
+ }
+ },
+ ],
+ "next_cursor": None,
+ }
+
+ mocker.patch("httpx.request", side_effect=[_mock_response(page)])
+
+ markdown = extractor._read_table_rows("tbl-1")
+
+ assert "| item1 | plain bold |" in markdown
+ # Every row must keep the same column count as the header.
+ for line in markdown.splitlines():
+ if line.startswith("|"):
+ assert line.count("|") == 3
+
+ def test_read_table_rows_preserves_empty_data_cells_as_columns(self, mocker: MockerFixture):
+ extractor = notion_extractor.NotionExtractor(
+ notion_workspace_id="ws",
+ notion_obj_id="obj",
+ notion_page_type="page",
+ tenant_id="tenant",
+ notion_access_token="token",
+ )
+
+ page = {
+ "results": [
+ {
+ "table_row": {
+ "cells": [
+ [{"text": {"content": "A"}}],
+ [{"text": {"content": "B"}}],
+ [{"text": {"content": "C"}}],
+ ]
+ }
+ },
+ {
+ "table_row": {
+ "cells": [
+ [{"text": {"content": "a1"}}],
+ [],
+ [{"text": {"content": "c1"}}],
+ ]
+ }
+ },
+ ],
+ "next_cursor": None,
+ }
+
+ mocker.patch("httpx.request", side_effect=[_mock_response(page)])
+
+ markdown = extractor._read_table_rows("tbl-1")
+
+ # The empty middle cell must remain an empty column instead of
+ # collapsing and shifting the following cell left.
+ assert "| a1 | | c1 |" in markdown
+
class TestNotionMetadataAndCredentialMethods:
def test_update_last_edited_time_no_document_model(self):
@@ -543,3 +626,18 @@ class TestNotionMetadataAndCredentialMethods:
monkeypatch.setattr(notion_extractor, "DatasourceProviderService", FakeProviderServiceFound)
assert notion_extractor.NotionExtractor._get_access_token("tenant", "cred") == "token-from-credential"
+
+
+def test_get_cell_text_uses_plain_text_for_mention_and_equation_segments():
+ # Notion rich text segments of type mention or equation carry plain_text
+ # but no "text" object; they must not be dropped from the cell content.
+ cell = [
+ {"type": "text", "text": {"content": "see "}, "plain_text": "see "},
+ {"type": "mention", "mention": {"type": "user", "user": {}}, "plain_text": "@alice"},
+ {"type": "equation", "equation": {"expression": "e=mc^2"}, "plain_text": "e=mc^2"},
+ ]
+ assert notion_extractor.NotionExtractor._get_cell_text(cell) == "see @alicee=mc^2"
+ # Empty cell stays an empty column.
+ assert notion_extractor.NotionExtractor._get_cell_text([]) == ""
+ # Fall back to text.content when plain_text is absent.
+ assert notion_extractor.NotionExtractor._get_cell_text([{"text": {"content": "x"}}]) == "x"