Skip to content

Commit

Permalink
throw error when vector and query are none
Browse files Browse the repository at this point in the history
Signed-off-by: hsm207 <[email protected]>
  • Loading branch information
hsm207 committed Mar 25, 2024
1 parent f939b75 commit 09f9336
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
9 changes: 7 additions & 2 deletions langchain_weaviate/vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,14 @@ def _perform_search(
):
kwargs["return_properties"].append(self._text_key)

# workaround to handle test_max_marginal_relevance_search
vector = kwargs.pop("vector", None)
if vector is None and query is not None:
if vector is None and query is None:
# raise an error because weaviate will do a fetch object query
# if both query and vector are None
raise ValueError("Either query or vector must be provided.")

# workaround to handle test_max_marginal_relevance_search
if vector is None:
vector = self._embedding.embed_query(query)

return_uuids = kwargs.pop("return_uuids", False)
Expand Down
22 changes: 22 additions & 0 deletions tests/integration_tests/test_vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,3 +802,25 @@ def run_similarity_test(search_method):
run_similarity_test("similarity_search")

run_similarity_test("similarity_search_with_score")


def test_invalid_search_param(weaviate_client, embedding_openai):
index_name = f"TestIndex_{uuid.uuid4().hex}"
text_key = "page"
weaviate_vector_store = WeaviateVectorStore(
weaviate_client, index_name, text_key, embedding_openai
)

with pytest.raises(ValueError) as excinfo:
weaviate_vector_store._perform_search(query=None, k=5)

assert str(excinfo.value) == "Either query or vector must be provided."

with pytest.raises(ValueError) as excinfo:
weaviate_vector_store._perform_search(query=None, vector=None, k=5)

assert str(excinfo.value) == "Either query or vector must be provided."

weaviate_vector_store._perform_search(query="hello", vector=None, k=5)

weaviate_vector_store._perform_search(query=None, vector=[1, 2, 3], k=5)

0 comments on commit 09f9336

Please sign in to comment.