Repository: mem0ai/mem0
File: embedchain/vectordb/weaviate.py (Python)

Code before:
            weaviate_where_clause = {"operator": "And", "operands": weaviate_where_operands}

        existing_ids = []
        metadatas = []
        cursor = None
        offset = 0
        has_iterated_once = False
        query_metadata_keys = self.metadata_keys.union(keys)
        while cursor is not None or not has_iterated_once:
            has_iterated_once = True
            results = self._query_with_offset(
                self.client.query.get(
                    self.index_name,
                    [
                        "identifier",
                        weaviate.LinkTo("metadata", self.index_name + "_metadata", list(query_metadata_keys)),
                    ],
                )
                .with_where(weaviate_where_clause)
                .with_additional(["id"])
                .with_limit(limit or self.BATCH_SIZE),
                offset,
            )

            fetched_results = results["data"]["Get"].get(self.index_name, [])
            if fetched_results is None or len(fetched_results) == 0:
                break

            for result in fetched_results:
                existing_ids.append(result["identifier"])
                metadatas.append(result["metadata"][0])
                cursor = result["_additional"]["id"]
                offset += 1

            if limit is not None and len(existing_ids) >= limit:
                break

        return {"ids": existing_ids, "metadatas": metadatas}

    def add(self, documents: list[str], metadatas: list[object], ids: list[str], **kwargs: Optional[dict[str, any]]):
        """add data in vector database
        :param documents: list of texts to add
        :type documents: list[str]
        :param metadatas: list of metadata associated with docs
        :type metadatas: list[object]
        :param ids: ids of docs
        :type ids: list[str]
        """
        embeddings = self.embedder.embedding_fn(documents)
        self.client.batch.configure(batch_size=self.BATCH_SIZE, timeout_retries=3)  # Configure batch
        with self.client.batch as batch:  # Initialize a batch process

Review comment (suggestion):
```suggestion
            if not fetched_results:
```

Code after:
            weaviate_where_clause = {"operator": "And", "operands": weaviate_where_operands}

        existing_ids = []
        metadatas = []
        cursor = None
        offset = 0
        has_iterated_once = False
        query_metadata_keys = self.metadata_keys.union(keys)
        while cursor is not None or not has_iterated_once:
            has_iterated_once = True
            results = self._query_with_offset(
                self.client.query.get(
                    self.index_name,
                    [
                        "identifier",
                        weaviate.LinkTo("metadata", self.index_name + "_metadata", list(query_metadata_keys)),
                    ],
                )
                .with_where(weaviate_where_clause)
                .with_additional(["id"])
                .with_limit(limit or self.BATCH_SIZE),
                offset,
            )

            fetched_results = results["data"]["Get"].get(self.index_name, [])
            if not fetched_results:
                break

            for result in fetched_results:
                existing_ids.append(result["identifier"])
                metadatas.append(result["metadata"][0])
                cursor = result["_additional"]["id"]
                offset += 1

            if limit is not None and len(existing_ids) >= limit:
                break

        return {"ids": existing_ids, "metadatas": metadatas}

    def add(self, documents: list[str], metadatas: list[object], ids: list[str], **kwargs: Optional[dict[str, any]]):
        """add data in vector database
        :param documents: list of texts to add
        :type documents: list[str]
        :param metadatas: list of metadata associated with docs
        :type metadatas: list[object]
        :param ids: ids of docs
        :type ids: list[str]
        """
        embeddings = self.embedder.embedding_fn(documents)
        self.client.batch.configure(batch_size=self.BATCH_SIZE, timeout_retries=3)  # Configure batch
        with self.client.batch as batch:  # Initialize a batch process