Skip to content
Open
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
13 changes: 11 additions & 2 deletions haystack/core/pipeline/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import heapq
from collections.abc import Callable
from copy import deepcopy
from copy import copy, deepcopy
from functools import wraps
from itertools import count
from typing import Any
Expand Down Expand Up @@ -42,7 +42,16 @@ def _deepcopy_with_exceptions(obj: Any) -> Any:
return type(obj)(_deepcopy_with_exceptions(v) for v in obj)

if isinstance(obj, dict):
return {k: _deepcopy_with_exceptions(v) for k, v in obj.items()}
# Rebuild the concrete dict subclass (e.g. ``defaultdict``, ``OrderedDict``, ``Counter``) instead of
# collapsing it to a plain ``dict``. ``type(obj)(<iterable>)`` is unsafe here because dict subclasses
# have incompatible constructors (``defaultdict`` takes ``default_factory`` first), so we copy the
# container and refill it, mirroring the ``type(obj)`` handling above. Assigning item by item avoids
# ``Counter.update`` interpreting the items as an iterable to count.
result = copy(obj)
result.clear()
for key, value in obj.items():
result[key] = _deepcopy_with_exceptions(value)
return result

# Components and Tools often contain objects that we do not want to deepcopy or are not deepcopyable
# (e.g. models, clients, etc.). In this case we return the object as-is.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
fixes:
- |
Fixed ``_deepcopy_with_exceptions`` collapsing ``dict`` subclasses such as
``defaultdict``, ``OrderedDict`` and ``Counter`` into plain ``dict``. The
function rebuilt every dict with a dict comprehension, so a component input or
pipeline parameter that contained a ``defaultdict`` lost its ``default_factory``
and a ``Counter`` came back as a plain ``dict``, even though the list/tuple/set
branch already preserved ``type(obj)``. Dict subclasses are now copied as their
own type (with deep-copied values), so ``Pipeline.run`` no longer changes the
type of the data a component receives.
23 changes: 22 additions & 1 deletion test/core/pipeline/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import logging
import warnings
from collections import namedtuple
from collections import Counter, OrderedDict, defaultdict, namedtuple
from functools import partial

import pytest

Expand Down Expand Up @@ -269,6 +270,26 @@ def test_deepcopy_with_fallback_namedtuple(self):
# Its contents are deep-copied, matching how plain tuples are handled.
assert copy["point"].x is not original["point"].x

@pytest.mark.parametrize("factory", [partial(defaultdict, list), OrderedDict, Counter])
def test_deepcopy_with_fallback_dict_subclass(self, factory):
container = factory()
container["a"] = [1, 2]
original = {"data": container}
copy = _deepcopy_with_exceptions(original)
# The concrete dict subclass survives the copy instead of collapsing to ``dict``.
assert type(copy["data"]) is type(original["data"])
assert copy["data"] == original["data"]
# The container and its contents are deep-copied, matching how plain dicts are handled.
assert copy["data"] is not original["data"]
assert copy["data"]["a"] is not original["data"]["a"]

def test_deepcopy_with_fallback_defaultdict_keeps_factory(self):
original = {"data": defaultdict(list, a=[1])}
copy = _deepcopy_with_exceptions(original)
assert copy["data"].default_factory is list
# A missing key still triggers the factory instead of raising KeyError.
assert copy["data"]["missing"] == []


class TestArgsDeprecated:
@pytest.fixture
Expand Down