Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Use this component in hybrid retrieval pipelines or indexing pipelines with mult

### On its own

Below is an example where we are using the `DocumentJoiner` to merge two lists of documents. We run the `DocumentJoiner` and provide the documents. It returns a list of documents ranked by combined scores. By default, equal weight is given to each Retriever score. You could also use custom weights by setting the weights parameter to a list of floats with one weight per input component.
Below is an example where we are using the `DocumentJoiner` to merge two lists of documents. We run the `DocumentJoiner` and provide the documents. It returns a list of documents ranked by combined scores. By default, equal weight is given to each Retriever score. You could also use custom weights by setting the weights parameter to a list of floats with one weight per input component. In a pipeline, `weights[i]` applies to the i-th connection made to `documents` with `Pipeline.connect()`.

```python
from haystack import Document
Expand Down
2 changes: 2 additions & 0 deletions haystack/components/joiners/document_joiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def __init__(
This parameter is ignored for
`concatenate`, `merge_embeddings`, or `distribution_based_rank_fusion` join modes.
Weight for each list of documents must match the number of inputs.
`weights[i]` applies to the documents from the i-th connection made to `documents`
with `Pipeline.connect()`.
Each weight must be a non-negative number.
:param top_k:
The maximum number of documents to return. Must be `None` or greater than 0.
Expand Down
48 changes: 47 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,36 @@ 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.

: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
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,21 @@
---
upgrade:
- |
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()``. Before, ``Pipeline.run()`` passed them in the
order the senders ran, so the results of order-dependent components such as ``DocumentJoiner`` with ``weights``
can change: connect the senders in the order you want the values to arrive.

If you load pipelines from YAML, check the order of your ``connections`` entries. Pipelines saved before this
release list ``connections`` grouped by sender, not in the order they were made, and the order of the entries
now decides the order in which a socket receives its values. For example, if ``embedding_retriever`` is listed
before ``bm25_retriever`` in ``connections``, ``DocumentJoiner`` now applies ``weights[0]`` to the embedding
retriever, where before it could apply it to the BM25 retriever. Reorder the entries that feed a ``Variadic``
or multi-sender ``list`` socket so the senders come in the order you expect.
fixes:
- |
``Pipeline.run_async()`` passed the values of a ``Variadic`` input, or a ``list`` input connected to several
senders, in the order the senders finished, which could change between runs, so ``DocumentJoiner`` could apply
its ``weights`` to the wrong retriever. Values now arrive in connection order, as in ``Pipeline.run()``, with
pipeline inputs passed directly to such a socket first. ``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