Skip to content
Merged
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
4 changes: 4 additions & 0 deletions haystack/components/preprocessors/text_cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ def run(self, texts: list[str]) -> dict[str, Any]:
:param texts: List of strings to clean.
:returns: A dictionary with the following key:
- `texts`: the cleaned list of strings.
:raises TypeError:
If `texts` is not a list or any element is not a string.
"""
if not isinstance(texts, list) or not all(isinstance(text, str) for text in texts):
raise TypeError("`texts` must be a list of strings.")

if self._regex:
texts = [self._regex.sub("", text) for text in texts]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
enhancements:
- |
``TextCleaner.run`` now raises a clear ``TypeError`` when ``texts`` is not a list or any element is not a ``str``,
instead of failing later or producing unexpected results.
12 changes: 12 additions & 0 deletions test/components/preprocessors/test_text_cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#
# SPDX-License-Identifier: Apache-2.0

import pytest

from haystack import Pipeline
from haystack.components.preprocessors import TextCleaner

Expand Down Expand Up @@ -31,6 +33,16 @@ def test_run_with_empty_inputs():
assert result["texts"] == []


def test_run_rejects_invalid_inputs():
cleaner = TextCleaner()

with pytest.raises(TypeError):
cleaner.run(texts=["Hello, World!", None, 42, ""]) # type: ignore[list-item]

with pytest.raises(TypeError):
cleaner.run(texts=15) # type: ignore[arg-type]


def test_run_with_regex():
cleaner = TextCleaner(remove_regexps=[r"\d+"])
result = cleaner.run(texts=["Open123 Source", "HaystackAI"])
Expand Down
Loading