Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community: Correctly handle multi-element rich text #25762

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions libs/community/langchain_community/document_loaders/notiondb.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,9 @@ def load_page(self, page_summary: Dict[str, Any]) -> Document:
prop_type = prop_data["type"]

if prop_type == "rich_text":
value = (
prop_data["rich_text"][0]["plain_text"]
if prop_data["rich_text"]
else None
)
value = self._concatenate_rich_text(prop_data["title"])
elif prop_type == "title":
value = (
prop_data["title"][0]["plain_text"] if prop_data["title"] else None
)
value = self._concatenate_rich_text(prop_data["title"])
elif prop_type == "multi_select":
value = (
[item["name"] for item in prop_data["multi_select"]]
Expand Down Expand Up @@ -228,3 +222,7 @@ def _request(
)
res.raise_for_status()
return res.json()

def _concatenate_rich_text(self, rich_text_array: List[Dict[str, Any]]) -> str:
"""Concatenate all text content from a rich_text array."""
return "".join(item["plain_text"] for item in rich_text_array)
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
from unittest.mock import patch

from langchain_core.documents import Document

from langchain_community.document_loaders.notion import NotionDBLoader


class TestNotionDBLoader:
def setup_method(self):
self.loader = NotionDBLoader(
integration_token="fake_token", database_id="fake_db_id"
)

def test_concatenate_rich_text(self):
# Setup
rich_text = [
{"plain_text": "Hello "},
{"plain_text": "world"},
{"plain_text": "!"},
]

# Exercise
result = self.loader._concatenate_rich_text(rich_text)

# Assert
assert result == "Hello world!"

@patch("langchain_community.document_loaders.notion.NotionDBLoader._request")
@patch("langchain_community.document_loaders.notion.NotionDBLoader._load_blocks")
def test_load_page_with_rich_text(self, mock_load_blocks, mock_request):
# Setup
mock_load_blocks.return_value = "Mocked block content"
page_summary = {
"id": "page_id",
"properties": {
"Title": {"type": "title", "title": [{"plain_text": "Test Title"}]},
"Description": {
"type": "rich_text",
"rich_text": [
{"plain_text": "This is "},
{"plain_text": "a test"},
{"plain_text": " description"},
],
},
},
}
expected_doc = Document(
page_content="Mocked block content",
metadata={
"title": "Test Title",
"description": "This is a test description",
"id": "page_id",
},
)

# Exercise
result = self.loader.load_page(page_summary)

# Assert
assert result == expected_doc

@patch("langchain_community.document_loaders.notion.NotionDBLoader._request")
@patch("langchain_community.document_loaders.notion.NotionDBLoader._load_blocks")
def test_load_page_with_code_in_rich_text(self, mock_load_blocks, mock_request):
# Setup
mock_load_blocks.return_value = "Mocked block content"
page_summary = {
"id": "page_id",
"properties": {
"Answer": {
"type": "rich_text",
"rich_text": [
{"plain_text": "Use "},
{"plain_text": "print('Hello')"},
{"plain_text": " to display text"},
],
}
},
}
expected_doc = Document(
page_content="Mocked block content",
metadata={"answer": "Use print('Hello') to display text", "id": "page_id"},
)

# Exercise
result = self.loader.load_page(page_summary)

# Assert
assert result == expected_doc

@patch("langchain_community.document_loaders.notion.NotionDBLoader._request")
@patch("langchain_community.document_loaders.notion.NotionDBLoader._load_blocks")
def test_load(self, mock_load_blocks, mock_request):
# Setup
mock_load_blocks.return_value = "Mocked block content"
mock_request.return_value = {
"results": [
{
"id": "page_id_1",
"properties": {
"Title": {
"type": "title",
"title": [{"plain_text": "Test Title 1"}],
}
},
},
{
"id": "page_id_2",
"properties": {
"Title": {
"type": "title",
"title": [{"plain_text": "Test Title 2"}],
}
},
},
],
"has_more": False,
}
expected_docs = [
Document(
page_content="Mocked block content",
metadata={"title": "Test Title 1", "id": "page_id_1"},
),
Document(
page_content="Mocked block content",
metadata={"title": "Test Title 2", "id": "page_id_2"},
),
]

# Exercise
result = self.loader.load()

# Assert
assert result == expected_docs
Loading