From bc80da9b44c73ff90f4e5ef201475dc507b85daf Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Fri, 19 Jan 2024 14:10:43 -0500 Subject: [PATCH] Improve error message for query top_k --- pinecone/data/index.py | 6 +++++- tests/unit/test_index.py | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pinecone/data/index.py b/pinecone/data/index.py index 6634ce72d..e9d7dc649 100644 --- a/pinecone/data/index.py +++ b/pinecone/data/index.py @@ -315,6 +315,7 @@ def fetch(self, ids: List[str], namespace: Optional[str] = None, **kwargs) -> Fe @validate_and_convert_errors def query( self, + *args, top_k: int, vector: Optional[List[float]] = None, id: Optional[str] = None, @@ -366,11 +367,14 @@ def query( and namespace name. """ - _check_type = kwargs.pop("_check_type", False) + if len(args) > 0: + raise ValueError("The argument order for `query()` has changed; please use keyword arguments instead of positional arguments. Example: index.query(vector=[0.1, 0.2, 0.3], top_k=10, namespace='my_namespace')") if vector is not None and id is not None: raise ValueError("Cannot specify both `id` and `vector`") + _check_type = kwargs.pop("_check_type", False) + sparse_vector = self._parse_sparse_values_arg(sparse_vector) args_dict = self._parse_non_empty_args( [ diff --git a/tests/unit/test_index.py b/tests/unit/test_index.py index 57ec7e6ca..5b1086e54 100644 --- a/tests/unit/test_index.py +++ b/tests/unit/test_index.py @@ -374,6 +374,11 @@ def test_query_rejects_both_id_and_vector(self): with pytest.raises(ValueError, match="Cannot specify both `id` and `vector`"): self.index.query(top_k=10, id="vec1", vector=[1, 2, 3]) + def test_query_with_positional_args(self, mocker): + with pytest.raises(ValueError) as e: + self.index.query([0.1, 0.2, 0.3], top_k=10) + assert "The argument order for `query()` has changed; please use keyword arguments instead of positional arguments" in str(e.value) + # endregion # region: delete tests