async def _async_prepare_kv_pairs(
        self, nodes: Sequence[BaseNode], allow_update: bool, store_text: bool
    ) -> Tuple[List[Tuple[str, dict]], List[Tuple[str, dict]], List[Tuple[str, dict]]]:
        """
        This method processes a sequence of document nodes asynchronously and prepares
        key-value pairs for nodes, their metadata, and reference documents. The key-value
        pairs are structured for subsequent insertion into the key-value store. This method
        does not insert the key-value pairs into the store; it only prepares them. The reference
        document key-value pairs are merged to ensure each `ref_doc_id` has a consolidated entry.

        Args:
            nodes (Sequence[BaseNode]): A sequence of document nodes to be processed.
            allow_update (bool): A flag indicating whether existing nodes should be updated.
            store_text (bool): A flag indicating whether the text content of the nodes should be stored.

        Returns:
            Tuple[
                list,          # List of key-value pairs for nodes
                list,          # List of key-value pairs for metadata
                List[Tuple[str, dict]]  # List of key-value pairs for reference documents, keyed by ref_doc_id
            ]

        Raises:
            ValueError: If a node already exists in the store and `allow_update` is False.

        """
        node_kv_pairs = []
        metadata_kv_pairs = []
        ref_doc_kv_pairs: Dict[str, List[Tuple[str, dict]]] = {}

        for node in nodes:
            # NOTE: doc could already exist in the store, but we overwrite it
            if not allow_update and await self.adocument_exists(node.node_id):
                raise ValueError(
                    f"node_id {node.node_id} already exists. "
                    "Set allow_update to True to overwrite."
                )
            ref_doc_info = None
            if isinstance(node, TextNode) and node.ref_doc_id is not None:
                ref_doc_info = (
                    await self.aget_ref_doc_info(node.ref_doc_id) or RefDocInfo()
                )

            (
                node_kv_pair,
                metadata_kv_pair,
                ref_doc_kv_pair,
            ) = self._get_kv_pairs_for_insert(node, ref_doc_info, store_text)

            if node_kv_pair is not None:
                node_kv_pairs.append(node_kv_pair)
            if metadata_kv_pair is not None:
                metadata_kv_pairs.append(metadata_kv_pair)
            if ref_doc_kv_pair is not None:
                key = ref_doc_kv_pair[0]
                if key not in ref_doc_kv_pairs:
                    ref_doc_kv_pairs[key] = []
                ref_doc_kv_pairs[key].append(ref_doc_kv_pair)

        # multiple nodes can point to the same ref_doc_id
        merged_ref_doc_kv_pairs = self._merge_ref_doc_kv_pairs(ref_doc_kv_pairs)

        return node_kv_pairs, metadata_kv_pairs, merged_ref_doc_kv_pairs

        This method processes a sequence of document nodes asynchronously and prepares
        key-value pairs for nodes, their metadata, and reference documents. The key-value
        pairs are structured for subsequent insertion into the key-value store. This method
        does not insert the key-value pairs into the store; it only prepares them. The reference
        document key-value pairs are merged to ensure each `ref_doc_id` has a consolidated entry.

        Args:
            nodes (Sequence[BaseNode]): A sequence of document nodes to be processed.
            allow_update (bool): A flag indicating whether existing nodes should be updated.
            store_text (bool): A flag indicating whether the text content of the nodes should be stored.

        Returns:
            Tuple[
                list,          # List of key-value pairs for nodes
                list,          # List of key-value pairs for metadata
                List[Tuple[str, dict]]  # List of key-value pairs for reference documents, keyed by ref_doc_id
            ]

        Raises:
            ValueError: If a node already exists in the store and `allow_update` is False.

        
llama-index-core/llama_index/core/storage/docstore/keyval_docstore.py