[PR #1671] fix: Incorrect order of embedded documents in CacheEmbedding #23163

Closed
opened 2026-02-21 20:20:33 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/1671

State: closed
Merged: Yes


Dify uses CacheEmbedding by default. When some documents have been embeded, those would not correspond to the order of text. The text and vectors stored in the vector database will not correspond.

class Weaviate(VectorStore):

    def add_texts(
        self,
        texts: Iterable[str],
        metadatas: Optional[List[dict]] = None,
        **kwargs: Any,
    ) -> List[str]:
        """Upload texts with metadata (properties) to Weaviate."""
        from weaviate.util import get_valid_uuid

        ids = []
        embeddings: Optional[List[List[float]]] = None
        if self._embedding:
            if not isinstance(texts, list):
                texts = list(texts)
            embeddings = self._embedding.embed_documents(texts)  # <--- when some documents have been embeded, those would not correspond to the order of text

        with self._client.batch as batch:
            for i, text in enumerate(texts): # <--- Loop according to text order
                data_properties = {self._text_key: text}
                if metadatas is not None:
                    for key, val in metadatas[i].items():
                        data_properties[key] = _json_serializable(val)

                # Allow for ids (consistent w/ other methods)
                # # Or uuids (backwards compatble w/ existing arg)
                # If the UUID of one of the objects already exists
                # then the existing object will be replaced by the new object.
                _id = get_valid_uuid(uuid4())
                if "uuids" in kwargs:
                    _id = kwargs["uuids"][i]
                elif "ids" in kwargs:
                    _id = kwargs["ids"][i]

                batch.add_data_object(
                    data_object=data_properties,
                    class_name=self._index_name,
                    uuid=_id,
                    vector=embeddings[i] if embeddings else None, # <--- In this case, a corresponding error will occur
                )
                ids.append(_id)

```



**Original Pull Request:** https://github.com/langgenius/dify/pull/1671 **State:** closed **Merged:** Yes --- Dify uses CacheEmbedding by default. When some documents have been embeded, those would not correspond to the order of text. The text and vectors stored in the vector database will not correspond. ````python class Weaviate(VectorStore): def add_texts( self, texts: Iterable[str], metadatas: Optional[List[dict]] = None, **kwargs: Any, ) -> List[str]: """Upload texts with metadata (properties) to Weaviate.""" from weaviate.util import get_valid_uuid ids = [] embeddings: Optional[List[List[float]]] = None if self._embedding: if not isinstance(texts, list): texts = list(texts) embeddings = self._embedding.embed_documents(texts) # <--- when some documents have been embeded, those would not correspond to the order of text with self._client.batch as batch: for i, text in enumerate(texts): # <--- Loop according to text order data_properties = {self._text_key: text} if metadatas is not None: for key, val in metadatas[i].items(): data_properties[key] = _json_serializable(val) # Allow for ids (consistent w/ other methods) # # Or uuids (backwards compatble w/ existing arg) # If the UUID of one of the objects already exists # then the existing object will be replaced by the new object. _id = get_valid_uuid(uuid4()) if "uuids" in kwargs: _id = kwargs["uuids"][i] elif "ids" in kwargs: _id = kwargs["ids"][i] batch.add_data_object( data_object=data_properties, class_name=self._index_name, uuid=_id, vector=embeddings[i] if embeddings else None, # <--- In this case, a corresponding error will occur ) ids.append(_id) ```
yindo added the pull-request label 2026-02-21 20:20:33 -05:00
yindo closed this issue 2026-02-21 20:20:33 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#23163