diff --git a/haystack/preview/components/preprocessors/text_document_splitter.py b/haystack/preview/components/preprocessors/text_document_splitter.py index f9f748b9d..76ede3da2 100644 --- a/haystack/preview/components/preprocessors/text_document_splitter.py +++ b/haystack/preview/components/preprocessors/text_document_splitter.py @@ -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: diff --git a/releasenotes/notes/fix-text-splitter-65870db474338749.yaml b/releasenotes/notes/fix-text-splitter-65870db474338749.yaml new file mode 100644 index 000000000..6633117e4 --- /dev/null +++ b/releasenotes/notes/fix-text-splitter-65870db474338749.yaml @@ -0,0 +1,3 @@ +--- +preview: + - Fix TextDocumentSplitter failing when run with an empty list diff --git a/test/preview/components/preprocessors/test_text_document_splitter.py b/test/preview/components/preprocessors/test_text_document_splitter.py index 900842333..ac55e3709 100644 --- a/test/preview/components/preprocessors/test_text_document_splitter.py +++ b/test/preview/components/preprocessors/test_text_document_splitter.py @@ -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):