Repository: jerryjliu/gpt_index
File: llama-index-core/llama_index/core/indices/vector_store/retrievers/retriever.py (Python)

Code before:
        elif query_result.ids:
            return [
                self._index.index_struct.nodes_dict[idx] for idx in query_result.ids
            ]
        else:
            raise ValueError(
                "Vector store query result should return at least one of nodes or ids."
            )

    def _insert_fetched_nodes_into_query_result(
        self, query_result: VectorStoreQueryResult, fetched_nodes: List[BaseNode]
    ) -> Sequence[BaseNode]:
        """
        Insert the fetched nodes into the query result.

        If the vector store does not store text, all nodes are inserted into the query result.
        If the vector store stores text, we replace non-text nodes with those fetched from the docstore.
        """
        fetched_nodes_by_id: Dict[str, BaseNode] = {
            node.node_id: node for node in fetched_nodes
        }

        new_nodes: List[BaseNode] = []

        if query_result.nodes:
            for node in list(query_result.nodes or []):
                if node.node_id in fetched_nodes_by_id:
                    new_nodes.append(fetched_nodes_by_id[node.node_id])
                    continue
                new_nodes.append(node)
        elif query_result.ids:
            for node_id in query_result.ids:
                new_nodes.append(fetched_nodes_by_id[node_id])
        else:
            raise ValueError(
                "Vector store query result should return at least one of nodes or ids."
            )

        return new_nodes

    def _convert_nodes_to_scored_nodes(
        self, query_result: VectorStoreQueryResult
    ) -> List[NodeWithScore]:
        """Create scored nodes from the vector store query result."""
        node_with_scores: List[NodeWithScore] = []

        for ind, node in enumerate(list(query_result.nodes or [])):
            score: Optional[float] = None
            if query_result.similarities is not None:
                score = query_result.similarities[ind]


Review comment (suggestion):
Since you already checked for `query_result.nodes` above, this `list(query_result.nodes or [])` should not be needed, if I understand it correctly 

Code after:
        elif query_result.ids:
            return [
                self._index.index_struct.nodes_dict[idx] for idx in query_result.ids
            ]
        else:
            raise ValueError(
                "Vector store query result should return at least one of nodes or ids."
            )

    def _insert_fetched_nodes_into_query_result(
        self, query_result: VectorStoreQueryResult, fetched_nodes: List[BaseNode]
    ) -> Sequence[BaseNode]:
        """
        Insert the fetched nodes into the query result.

        If the vector store does not store text, all nodes are inserted into the query result.
        If the vector store stores text, we replace non-text nodes with those fetched from the docstore.
        """
        fetched_nodes_by_id: Dict[str, BaseNode] = {
            node.node_id: node for node in fetched_nodes
        }

        new_nodes: List[BaseNode] = []

        if query_result.nodes:
            for node in list(query_result.nodes):
                if node.node_id in fetched_nodes_by_id:
                    new_nodes.append(fetched_nodes_by_id[node.node_id])
                    continue
                new_nodes.append(node)
        elif query_result.ids:
            for node_id in query_result.ids:
                new_nodes.append(fetched_nodes_by_id[node_id])
        else:
            raise ValueError(
                "Vector store query result should return at least one of nodes or ids."
            )

        return new_nodes

    def _convert_nodes_to_scored_nodes(
        self, query_result: VectorStoreQueryResult
    ) -> List[NodeWithScore]:
        """Create scored nodes from the vector store query result."""
        node_with_scores: List[NodeWithScore] = []

        for ind, node in enumerate(list(query_result.nodes or [])):
            score: Optional[float] = None
            if query_result.similarities is not None:
                score = query_result.similarities[ind]