Skip to content

vertexai: fix handling of single string in add_texts #860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,11 @@ def add_texts(

# Makes sure is a list and can get the length, should we support iterables?
# metadata is a list so probably not?
texts = list(texts)
if isinstance(texts, str):
texts = [texts]
else:
texts = list(texts)

embeddings = self._embeddings.embed_documents(texts)

return self.add_texts_with_embeddings(
Expand Down
14 changes: 14 additions & 0 deletions libs/vertexai/tests/unit_tests/test_vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ def test_add_texts_with_custom_ids(mocker):
VectorStore.add_texts(texts=texts, ids=["Id1", "Id2", "Id2"])


def test_add_texts_with_single_string():
"""Test that a single string input is properly handled as one document."""
single_string = "This is a single string"

VectorStore = object.__new__(_BaseVertexAIVectorStore)
VectorStore._document_storage = MagicMock()
VectorStore._embeddings = MagicMock()
VectorStore._searcher = MagicMock()

VectorStore.add_texts(texts=single_string)

VectorStore._embeddings.embed_documents.assert_called_once_with([single_string])


def test_add_texts_with_embeddings():
texts = ["Text1", "Text2"]
embeddings = [[0.1, 0.2, 1.0], [1.0, 0.0, 1.0]]
Expand Down
Loading