Support merging dense and sparse embeddings from parallel pipeline branches
Is your feature request related to a problem? Please describe.
Running dense and sparse document embedders concurrently produces separate copies of each document, containing only their respective embeddings.
Connecting both branches to a DocumentWriter does not combine these fields. With DuplicatePolicy.OVERWRITE, one copy replaces the other, leaving embedding or sparse_embedding as None. DuplicatePolicy.OVERWRITE is the default policy in a lot of document stores so this issue is common.
Describe the solution you'd like
Have Haystack pipelines automatically merge documents with the same ID when branches converge. This would allow dense and sparse embedders to run concurrently and connect directly to a DocumentWriter, which would receive one document per ID containing both embeddings.
Describe alternatives you've considered
- Connecting dense embedder → sparse embedder → writer preserves both fields, but forces the embedders to run sequentially.
- Changing duplicate policy, however this still means two writes happen to the document store and not every store supports merging duplicate policy.
Additional context
Reproduced using haystack-ai==3.3.0 and huggingface-api-haystack==2.1.0:
import asyncio
from haystack import Document, Pipeline
from haystack.components.writers import DocumentWriter
from haystack.dataclasses import SparseEmbedding
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.document_stores.types import DuplicatePolicy
from haystack_integrations.components.embedders.huggingface_api import (
HuggingFaceAPIDocumentEmbedder,
HuggingFaceTEISparseDocumentEmbedder,
)
async def main():
store = InMemoryDocumentStore()
dense = HuggingFaceAPIDocumentEmbedder(api_type="text_embeddings_inference", api_params={"url": "http://localhost:8080"})
sparse = HuggingFaceTEISparseDocumentEmbedder(api_base_url="http://localhost:8081")
pipeline = Pipeline()
pipeline.add_component("dense", dense)
pipeline.add_component("sparse", sparse)
pipeline.add_component("writer", DocumentWriter(store, policy=DuplicatePolicy.OVERWRITE))
pipeline.connect("dense.documents", "writer.documents")
pipeline.connect("sparse.documents", "writer.documents")
documents = [
Document(id="doc-1", content="Paris is the capital of France.")
]
await pipeline.run_async({"dense": {"documents": documents}, "sparse": {"documents": documents}})
stored = store.filter_documents()[0]
print(stored.embedding, stored.sparse_embedding)
# One field is None; which survives depends on write order.
assert (stored.embedding is None) != (
stored.sparse_embedding is None
)
asyncio.run(main())
👋 Hello there! This issue will be handled internally and isn't open for external contributions. If you'd like to contribute, please take a look at issues labeled contributions welcome or good first issue. We'd really appreciate it!
hello, i would like to work on this
@31groot Thanks for the initiative! We'll handle this one internally, thanks!
@maxdswain, there are several ways to implement this feature, and I'm looking into them.
One idea I am considering: add a new join_mode to DocumentJoiner. Something like DocumentJoiner(join_mode="merge_embeddings"), which would combine embeddings from documents with the same ID. This seems quite aligned with other use cases we support on this component.
This would enable the behavior you suggest. WDYT?
Hi @anakin87 thanks for asking. Could embedding merging happen automatically, similar to the existing implicit list joining when connecting multiple retrievers to a ranker? I believe that was implemented in 2.24.0.
I'd prefer not to merge embeddings at the pipeline level. I would find it a bit surprising for users and we'd need to handle conflicting fields.
I still prefer an explicitDocumentJoiner mode. It makes the transformation visible and users can easily inspect the merged documents.
Another idea is a merge_embeddings option in the DocumentWriter: this would work without a separate component, but the merged documents would be harder to inspect and it also feels more convoluted from the implementation point of view.
@sjrl WDYT?
I think the new mode in DocumentJoiner also makes the most sense. The implicit list joining is only for appending lists, it wasn't intended to handle merging of contents within the two lists being combined. And I'd prefer to keep that implicit list joining "dumb" and only do a naive list append.
This is why in all of our rankers we added document de-duplication as the first step since the naive list append doesn't handle removing duplicate documents based on ID.
Sure if that's how you think is best, I have no problem with that!