Skip to content

Commit ff4f525

Browse files
authored
Improve error message when using positional arguments with query() (#292)
## Problem In v3.0, the order of arguments to `query()` was changed to reflect that `top_k` is required and should therefore come first in the list of parameters. This is unfortunate, since it means any usage relying on `vector` being the first positional parameter will see a cryptic error about passing two values of `top_k`. ## Solution Disable positional arguments for this function. This allows us to throw an error message with clearer instructions to the user. ## Type of Change - [x] None of the above: Error UX ## Test Plan Added unit test. ``` >>> from pinecone import Pinecone >>> pc = Pinecone(api_key='XXX') >>> index = pc.Index('foo') >>> index.query([0.1, 0.2], top_k=10) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/jhamon/workspace/pinecone-python-client/pinecone/utils/error_handling.py", line 10, in inner_func return func(*args, **kwargs) File "/Users/jhamon/workspace/pinecone-python-client/pinecone/data/index.py", line 373, in query raise ValueError("""The argument order for `query()` has changed; please use keyword arguments instead of positional arguments. 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') >>> index.query(vector=[0.1, 0.2], top_k=10) {"results":[],"matches":[],"namespace":"","usage":{"readUnits":0}} ```
1 parent e770c5a commit ff4f525

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

pinecone/data/index.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ def fetch(self, ids: List[str], namespace: Optional[str] = None, **kwargs) -> Fe
315315
@validate_and_convert_errors
316316
def query(
317317
self,
318+
*args,
318319
top_k: int,
319320
vector: Optional[List[float]] = None,
320321
id: Optional[str] = None,
@@ -366,11 +367,14 @@ def query(
366367
and namespace name.
367368
"""
368369

369-
_check_type = kwargs.pop("_check_type", False)
370+
if len(args) > 0:
371+
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')")
370372

371373
if vector is not None and id is not None:
372374
raise ValueError("Cannot specify both `id` and `vector`")
373375

376+
_check_type = kwargs.pop("_check_type", False)
377+
374378
sparse_vector = self._parse_sparse_values_arg(sparse_vector)
375379
args_dict = self._parse_non_empty_args(
376380
[

tests/unit/test_index.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ def test_query_rejects_both_id_and_vector(self):
374374
with pytest.raises(ValueError, match="Cannot specify both `id` and `vector`"):
375375
self.index.query(top_k=10, id="vec1", vector=[1, 2, 3])
376376

377+
def test_query_with_positional_args(self, mocker):
378+
with pytest.raises(ValueError) as e:
379+
self.index.query([0.1, 0.2, 0.3], top_k=10)
380+
assert "The argument order for `query()` has changed; please use keyword arguments instead of positional arguments" in str(e.value)
381+
377382
# endregion
378383

379384
# region: delete tests

0 commit comments

Comments
 (0)