Skip to content
50 changes: 49 additions & 1 deletion haystack/core/pipeline/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,22 @@ def to_dict(self) -> dict[str, Any]:
for name, instance in self.graph.nodes(data="instance"):
components[name] = component_to_dict(instance, name)

edges = list(self.graph.edges.data())
# graph.edges lists connections by sender, so connections into the same socket are put back in the order they
# were made: that order is the order in which a variadic socket receives its inputs
slots_by_socket: dict[tuple[str, str], list[int]] = {}
for index, (_, receiver, edge_data) in enumerate(edges):
slots_by_socket.setdefault((receiver, edge_data["to_socket"].name), []).append(index)
for slots in slots_by_socket.values():
in_connection_order = sorted(
(edges[slot] for slot in slots),
key=lambda edge: _connection_index(socket=edge[2]["to_socket"], sender=edge[0]),
)
for slot, edge in zip(slots, in_connection_order, strict=True):
edges[slot] = edge

connections = []
for sender, receiver, edge_data in self.graph.edges.data():
for sender, receiver, edge_data in edges:
sender_socket = edge_data["from_socket"].name
receiver_socket = edge_data["to_socket"].name
connections.append({"sender": f"{sender}.{sender_socket}", "receiver": f"{receiver}.{receiver_socket}"})
Expand Down Expand Up @@ -1348,6 +1362,8 @@ def _consume_component_inputs(
greedy_inputs_to_remove = set()
for socket_name, socket in component["input_sockets"].items():
socket_inputs = component_inputs.get(socket_name, [])
if socket.is_lazy_variadic:
socket_inputs = _sort_by_connection_order(socket=socket, socket_inputs=socket_inputs)
socket_inputs_values = [
sock["value"] for sock in socket_inputs if not isinstance(sock["value"], _NoOutputProduced)
]
Expand Down Expand Up @@ -1950,6 +1966,38 @@ def _validate_component_output_keys(
)


def _sort_by_connection_order(socket: InputSocket, socket_inputs: list[dict[str, Any]]) -> list[dict[str, Any]]:
"""
Sorts the inputs of a lazy variadic socket in the order their senders were connected to it.

Senders write to the socket in the order they finish, which varies between runs in `Pipeline.run_async()`.
Connection order is the same in both run modes. The sort is stable, so values from the same sender keep the order
they were written in.

:param socket: The lazy variadic input socket.
:param socket_inputs: The inputs written to the socket, as `{"sender": ..., "value": ...}` entries.
:returns: The inputs sorted by connection order.
"""
return sorted(socket_inputs, key=lambda sock: _connection_index(socket=socket, sender=sock["sender"]))


def _connection_index(socket: InputSocket, sender: str | None) -> int:
"""
Returns the position of a sender in the order senders were connected to a socket.

User inputs (`sender` is None) sort before every sender, and senders the socket doesn't list sort after them.

:param socket: The input socket.
:param sender: The name of the sending component, or None for a user input.
:returns: The sender's position in `socket.senders`.
"""
if sender is None:
return -1
if sender not in socket.senders:
Comment thread
sjrl marked this conversation as resolved.
Resolved
return len(socket.senders)
return socket.senders.index(sender)


def _write_to_lazy_variadic_socket(
inputs: InputsType, receiver_name: str, receiver_socket_name: str, component_name: str, value: Any
) -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
fixes:
Comment thread
sjrl marked this conversation as resolved.
Resolved
- |
Components with a ``Variadic`` input, or a ``list`` input connected to several senders, now receive the values
in the order the senders were connected with ``Pipeline.connect()``, in both ``Pipeline.run()`` and
``Pipeline.run_async()``. Pipeline inputs passed directly to such a socket come first.
Before, ``Pipeline.run_async()`` passed the values in the order the senders finished, which could change between
runs, so ``DocumentJoiner`` could apply its ``weights`` to the wrong retriever. ``Pipeline.run()`` passed them in
the order the senders ran, so results of order-dependent components such as ``DocumentJoiner`` can change for
``Pipeline.run()`` too: connect the senders in the order you want the values to arrive.
``Pipeline.to_dict()`` now saves connections in the order they were made, so a pipeline loaded with
``Pipeline.from_dict()`` keeps the same order.
Loading
Loading