Skip to content

The agent engineering platform.

core[minor]: Add get_by_ids to vectorstore interface (#23594)

Eugene Yurtsev4f1821db3e60
diff --git a/libs/core/langchain_core/vectorstores.py b/libs/core/langchain_core/vectorstores.py
index 9098538590..d846500b5b 100644
--- a/libs/core/langchain_core/vectorstores.py
+++ b/libs/core/langchain_core/vectorstores.py
@@ -34,6 +34,7 @@ from typing import (
Iterable,
List,
Optional,
+ Sequence,
Tuple,
Type,
TypeVar,
@@ -99,6 +100,57 @@ class VectorStore(ABC):
raise NotImplementedError("delete method must be implemented by subclass.")
+ def get_by_ids(self, ids: Sequence[str], /) -> List[Document]:
+ """Get documents by their IDs.
+
+ The returned documents are expected to have the ID field set to the ID of the
+ document in the vector store.
+
+ Fewer documents may be returned than requested if some IDs are not found or
+ if there are duplicated IDs.
+
+ Users should not assume that the order of the returned documents matches
+ the order of the input IDs. Instead, users should rely on the ID field of the
+ returned documents.
+
+ This method should **NOT** raise exceptions if no documents are found for
+ some IDs.
+
+ Args:
+ ids: List of ids to retrieve.
+
+ Returns:
+ List of Documents.
+ """
+ raise NotImplementedError(
+ f"{self.__class__.__name__} does not yet support get_by_ids."
+ )
+
+ # Implementations should override this method to provide an async native version.
+ async def aget_by_ids(self, ids: Sequence[str], /) -> List[Document]:
+ """Get documents by their IDs.
+
+ The returned documents are expected to have the ID field set to the ID of the
+ document in the vector store.
+
+ Fewer documents may be returned than requested if some IDs are not found or
+ if there are duplicated IDs.
+
+ Users should not assume that the order of the returned documents matches
+ the order of the input IDs. Instead, users should rely on the ID field of the
+ returned documents.
+
+ This method should **NOT** raise exceptions if no documents are found for
+ some IDs.
+
+ Args:
+ ids: List of ids to retrieve.
+
+ Returns:
+ List of Documents.
+ """
+ return await run_in_executor(None, self.get_by_ids, ids)
+
async def adelete(
self, ids: Optional[List[str]] = None, **kwargs: Any
) -> Optional[bool]: