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
9 changes: 3 additions & 6 deletions haystack/components/converters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ def normalize_metadata(meta: dict[str, Any] | list[dict[str, Any]] | None, sourc

:param meta: the meta input of the converter, as-is
:param sources_count: the number of sources the converter received
:returns: a list of dictionaries of the make length as the sources list

Each source always gets its own independent dictionary. When ``meta`` is ``None`` or a single
dictionary, a separate copy is returned for every source so that mutating one source's metadata
downstream does not leak into the others.
:returns: One independent metadata dictionary per source. If ``meta`` is ``None``, each source
gets an empty dictionary. Otherwise, the metadata for each source is deep-copied.
"""
if meta is None:
return [{} for _ in range(sources_count)]
Expand All @@ -78,5 +75,5 @@ def normalize_metadata(meta: dict[str, Any] | list[dict[str, Any]] | None, sourc
if isinstance(meta, list):
if sources_count != len(meta):
raise ValueError("The length of the metadata list must match the number of sources.")
return meta
return [deepcopy(item) for item in meta]
raise ValueError("meta must be either None, a dictionary or a list of dictionaries.")
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixed metadata sharing when passing a list of dictionaries to a converter's ``meta`` parameter.
Each source now receives an independent copy of its metadata, even when the same dictionary is
supplied for multiple sources.
15 changes: 15 additions & 0 deletions test/components/converters/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ def test_normalize_metadata_list_of_right_size():
assert normalize_metadata([{"a": 1}, {"b": 2}, {"c": 3}], sources_count=3) == [{"a": 1}, {"b": 2}, {"c": 3}]


@pytest.mark.parametrize(
"metadata",
[
pytest.param([{"tags": ["original"]}, {"tags": ["original"]}], id="distinct_dicts"),
pytest.param([{"tags": ["original"]}] * 2, id="repeated_dict"),
],
)
def test_normalize_metadata_list_returns_independent_copies(metadata):
result = normalize_metadata(metadata, sources_count=2)
result[0]["tags"].append("updated")

assert result == [{"tags": ["original", "updated"]}, {"tags": ["original"]}]
assert metadata == [{"tags": ["original"]}, {"tags": ["original"]}]


def test_normalize_metadata_list_of_wrong_size():
with pytest.raises(ValueError, match="The length of the metadata list must match the number of sources."):
normalize_metadata([{"a": 1}], sources_count=3)
Expand Down
Loading