fix: Fix TextDocumentSplitter failing if run with empty list (#6081)

* Fix TextDocumentSplitter failing if run with empty list

* Release notes

* Simplify check

* Enhance test
This commit is contained in:
Silvano Cerza 2023-10-17 11:25:28 +02:00 committed by GitHub
parent 90ddeba579
commit ec9f898cd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 4 deletions

View File

@ -44,8 +44,9 @@ class TextDocumentSplitter:
:return: A list of documents with the split texts.
"""
if not documents or not isinstance(documents, list) or not isinstance(documents[0], Document):
if not isinstance(documents, list) or (documents and not isinstance(documents[0], Document)):
raise TypeError("TextDocumentSplitter expects a List of Documents as input.")
split_docs = []
for doc in documents:
if doc.text is None:

View File

@ -0,0 +1,3 @@
---
preview:
- Fix TextDocumentSplitter failing when run with an empty list

View File

@ -21,9 +21,9 @@ class TestTextDocumentSplitter:
@pytest.mark.unit
def test_empty_list(self):
with pytest.raises(TypeError, match="TextDocumentSplitter expects a List of Documents as input."):
splitter = TextDocumentSplitter()
splitter.run(documents=[])
splitter = TextDocumentSplitter()
res = splitter.run(documents=[])
assert res == {"documents": []}
@pytest.mark.unit
def test_unsupported_split_by(self):