Skip to content
Merged
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
7 changes: 5 additions & 2 deletions pinecone/core/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def dump_numpy_public(np_array: 'np.ndarray', compressed: bool = False) -> 'vect
"""
Dump numpy array to vector_column_service_pb2.NdArray
"""
warn_deprecated('dump_numpy_public and all numpy-related features will be removed in a future version', deprecated_in='2.2.1', removal_in='3.0.0')
protobuf_arr = vector_column_service_pb2.NdArray()
protobuf_arr.dtype = str(np_array.dtype)
protobuf_arr.shape.extend(np_array.shape)
Expand All @@ -38,6 +39,7 @@ def dump_numpy_public(np_array: 'np.ndarray', compressed: bool = False) -> 'vect


def dump_strings_public(strs: List[str], compressed: bool = False) -> 'vector_column_service_pb2.NdArray':
warn_deprecated('dump_strings_public and all numpy-related features will be removed in a future version', deprecated_in='2.2.1', removal_in='3.0.0')
return dump_numpy_public(np.array(strs, dtype='S'), compressed=compressed)


Expand Down Expand Up @@ -90,6 +92,7 @@ def load_numpy_public(proto_arr: 'vector_column_service_pb2.NdArray') -> 'np.nda
:param proto_arr:
:return:
"""
warn_deprecated('load_numpy_public and all numpy-related features will be removed in a future version', deprecated_in='2.2.1', removal_in='3.0.0')
if len(proto_arr.shape) == 0:
return np.array([])
if proto_arr.compressed:
Expand All @@ -100,9 +103,9 @@ def load_numpy_public(proto_arr: 'vector_column_service_pb2.NdArray') -> 'np.nda


def load_strings_public(proto_arr: 'vector_column_service_pb2.NdArray') -> List[str]:
warn_deprecated('load_strings_public and all numpy-related features will be removed in a future version', deprecated_in='2.2.1', removal_in='3.0.0')
return [str(item, 'utf-8') for item in load_numpy_public(proto_arr)]


def warn_deprecated(description: str = '', deprecated_in: str = None, removal_in: str = None):
message = f'DEPRECATED since v{deprecated_in} [Will be removed in v{removal_in}]: {description}'
warnings.warn(message, DeprecationWarning)
warnings.warn(message, FutureWarning)
10 changes: 8 additions & 2 deletions pinecone/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ScoredVector, SingleQueryResults, DescribeIndexStatsResponse, UpsertRequest, UpsertResponse, UpdateRequest, \
Vector, DeleteRequest, UpdateRequest, DescribeIndexStatsRequest
from pinecone.core.client.api.vector_operations_api import VectorOperationsApi
from pinecone.core.utils import fix_tuple_length, get_user_agent
from pinecone.core.utils import fix_tuple_length, get_user_agent, warn_deprecated
import copy

__all__ = [
Expand All @@ -33,7 +33,6 @@
'_check_input_type', '_check_return_type', '_host_index', 'async_req'
)


def parse_query_response(response: QueryResponse, unary_query: bool):
if unary_query:
response._data_store.pop('results', None)
Expand All @@ -42,6 +41,10 @@ def parse_query_response(response: QueryResponse, unary_query: bool):
response._data_store.pop('namespace', None)
return response

def upsert_numpy_deprecation_notice(context):
numpy_deprecataion_notice = "The ability to pass a numpy ndarray as part of a dictionary argument to upsert() will be removed in a future version of the pinecone client. To remove this warning, use the numpy.ndarray.tolist method to convert your ndarray into a python list before calling upsert()."
message = " ".join([context, numpy_deprecataion_notice])
warn_deprecated(message, deprecated_in='2.2.1', removal_in='3.0.0')

class Index(ApiClient):

Expand Down Expand Up @@ -187,8 +190,10 @@ def _dict_to_vector(item):
values = item['sparse_values'].get('values', None)

if isinstance(values, np.ndarray):
upsert_numpy_deprecation_notice("Deprecated type passed in sparse_values['values'].")
values = values.tolist()
if isinstance(indices, np.ndarray):
upsert_numpy_deprecation_notice("Deprecated type passed in sparse_values['indices'].")
indices = indices.tolist()
try:
item['sparse_values'] = SparseValues(indices=indices, values=values)
Expand All @@ -203,6 +208,7 @@ def _dict_to_vector(item):
raise TypeError(f"Column `metadata` is expected to be a dictionary, found {type(metadata)}")

if isinstance(item['values'], np.ndarray):
upsert_numpy_deprecation_notice("Deprecated type passed in 'values'.")
item['values'] = item['values'].tolist()

try:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ dnspython>=2.0.0
python_dateutil >= 2.5.3
urllib3 >= 1.21.1
tqdm >= 4.64.1
numpy
numpy >= 1.22.0
13 changes: 11 additions & 2 deletions tests/unit/test_index.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pandas as pd
import numpy as np
import pytest
import warnings

from pinecone.core.client.api_client import Endpoint

Expand Down Expand Up @@ -34,8 +36,15 @@ def setup_method(self):

pinecone.init(api_key='example-key')
self.index = pinecone.Index('example-name')

# region: upsert tests

def test_upsert_numpy_deprecation_warning(self, mocker):
mocker.patch.object(self.index._vector_api, 'upsert', autospec=True)
with pytest.warns(FutureWarning):
# numpy used in dictionary values
self.index.upsert([{'id': '2', 'values': np.array([3.0, 3.2, 4.2])}])
with pytest.warns(FutureWarning):
# numpy used in dictionary sparse_values
self.index.upsert([{'id': '3', 'values': [0.5,0.5,0.5], 'sparse_values': { 'indices': np.array([0, 1]), 'values': np.array([3.0, 3.2])}}])

def test_upsert_tuplesOfIdVec_UpserWithoutMD(self, mocker):
mocker.patch.object(self.index._vector_api, 'upsert', autospec=True)
Expand Down