Skip to content

Commit 18a0fe6

Browse files
committed
fix(converters): avoid infinite loop
Signed-off-by: Arya Tayshete <[email protected]>
1 parent 3b907fa commit 18a0fe6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

haystack/components/converters/csv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def _build_document_from_row(
222222
suffix = 1
223223
while key_to_use in row_meta:
224224
key_to_use = f"{base_key}_{suffix}"
225-
suffix = 1
225+
suffix += 1
226226
row_meta[key_to_use] = self._safe_value(v)
227227

228228
row_meta["row_number"] = row_index

test/components/converters/test_csv_to_document.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,30 @@ def test_row_mode_meta_collision_prefixed(self, tmp_path: Path):
142142
assert d.meta["csv_encoding"] == "latin1"
143143
assert d.meta["comment"] == "ok"
144144

145+
def test_row_mode_meta_collision_multiple_suffixes(self, tmp_path):
146+
"""
147+
If meta already has csv_file_path and csv_file_path_1, we should write the next as csv_file_path_2 (not loop).
148+
"""
149+
csv_text = "file_path,comment\r\nrow.csv,ok\r\n"
150+
f = tmp_path / "multi.csv"
151+
f.write_text(csv_text, encoding="utf-8")
152+
153+
bs = ByteStream.from_file_path(f)
154+
bs.meta["file_path"] = str(f)
155+
156+
# Pre-seed meta so we force two collisions.
157+
extra_meta = {"csv_file_path": "existing0", "csv_file_path_1": "existing1"}
158+
159+
conv = CSVToDocument(conversion_mode="row")
160+
out = conv.run(sources=[bs], meta=[extra_meta])
161+
d = out["documents"][0]
162+
163+
# Existing values preserved; new one goes to _2
164+
assert d.meta["csv_file_path"] == "existing0"
165+
assert d.meta["csv_file_path_1"] == "existing1"
166+
assert d.meta["csv_file_path_2"] == "row.csv"
167+
assert d.meta["comment"] == "ok"
168+
145169
def test_init_validates_delimiter_and_quotechar(self):
146170
with pytest.raises(ValueError):
147171
CSVToDocument(delimiter=";;")

0 commit comments

Comments
 (0)