Skip to content

Commit d44b936

Browse files
authored
Add query() validation to prevent sending both id and vector (#285)
## Problem Currently the python SDK allows users to send a request that contains values for both the `id` and `vector` fields. This request doesn't make sense to the API since the `id` field, when provided, is used to look up a vector in the index whose values should be used as the query. We'd like to give clear feedback to the user that this is not correct usage rather than showing empty results. ## Solution - Implement the validation as a simple if-statement. - Update docstrings. ## Type of Change - [x] None of the above: UX improvement ## Test Plan Added unit tests for this error handling case
1 parent bd4dea2 commit d44b936

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

pinecone/data/index.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ def query(
368368

369369
_check_type = kwargs.pop("_check_type", False)
370370

371+
if vector is not None and id is not None:
372+
raise ValueError("Cannot specify both `id` and `vector`")
373+
371374
sparse_vector = self._parse_sparse_values_arg(sparse_vector)
372375
args_dict = self._parse_non_empty_args(
373376
[

pinecone/grpc/index_grpc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,10 @@ def query(
315315
Args:
316316
vector (List[float]): The query vector. This should be the same length as the dimension of the index
317317
being queried. Each `query()` request can contain only one of the parameters
318-
`queries`, `id` or `vector`.. [optional]
318+
`id` or `vector`.. [optional]
319319
id (str): The unique ID of the vector to be used as a query vector.
320320
Each `query()` request can contain only one of the parameters
321-
`queries`, `vector`, or `id`.. [optional]
321+
`vector` or `id`.. [optional]
322322
top_k (int): The number of results to return for each query. Must be an integer greater than 1.
323323
namespace (str): The namespace to fetch vectors from.
324324
If not specified, the default namespace is used. [optional]
@@ -337,7 +337,8 @@ def query(
337337
and namespace name.
338338
"""
339339

340-
queries = None
340+
if vector is not None and id is not None:
341+
raise ValueError("Cannot specify both `id` and `vector`")
341342

342343
if filter is not None:
343344
filter_struct = dict_to_proto_struct(filter)
@@ -349,7 +350,6 @@ def query(
349350
[
350351
("vector", vector),
351352
("id", id),
352-
("queries", queries),
353353
("namespace", namespace),
354354
("top_k", top_k),
355355
("filter", filter_struct),

tests/unit/test_index.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ def test_query_byVecId_queryByVecId(self, mocker):
370370
pinecone.QueryRequest(top_k=10, id="vec1", include_metadata=True, include_values=False)
371371
)
372372

373+
def test_query_rejects_both_id_and_vector(self):
374+
with pytest.raises(ValueError, match="Cannot specify both `id` and `vector`"):
375+
self.index.query(top_k=10, id="vec1", vector=[1, 2, 3])
376+
373377
# endregion
374378

375379
# region: delete tests

tests/unit_grpc/test_grpc_index_query.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ def test_query_byVecId_queryByVecId(self, mocker):
4242
QueryRequest(top_k=10, id="vec1", include_metadata=True, include_values=False),
4343
timeout=None,
4444
)
45+
46+
def test_query_rejects_both_id_and_vector(self):
47+
with pytest.raises(ValueError, match="Cannot specify both `id` and `vector`"):
48+
self.index.query(top_k=10, id="vec1", vector=[1, 2, 3])

0 commit comments

Comments
 (0)