Skip to content

Commit 3131191

Browse files
committed
finalize list() and extend PluginAware in NamespaceResource
1 parent ce47fb6 commit 3131191

File tree

9 files changed

+397
-264
lines changed

9 files changed

+397
-264
lines changed

pinecone/db_data/index.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pinecone.core.openapi.db_data.model.list_namespaces_response import ListNamespacesResponse
12
from pinecone.utils.tqdm import tqdm
23
import warnings
34
import logging
@@ -8,7 +9,6 @@
89

910
from pinecone.openapi_support import ApiClient
1011
from pinecone.core.openapi.db_data.api.vector_operations_api import VectorOperationsApi
11-
from pinecone.core.openapi.db_data.api.namespace_operations_api import NamespaceOperationsApi
1212
from pinecone.core.openapi.db_data import API_VERSION
1313
from pinecone.core.openapi.db_data.models import (
1414
QueryResponse,
@@ -630,10 +630,11 @@ def delete_namespace(self, namespace: str) -> Dict[str, Any]:
630630
return self.namespace.delete(namespace=namespace)
631631

632632
@validate_and_convert_errors
633-
def list_namespaces(self,
634-
limit: Optional[int] = None,
635-
pagination_token: Optional[str] = None,
636-
) -> Iterator["NamespaceDescription"]:
637-
return self.namespace.list(limit=limit, pagination_token=pagination_token)
633+
def list_namespaces(self, **kwargs) -> Iterator[ListNamespacesResponse]:
634+
return self.namespace.list(**kwargs)
638635

639-
636+
@validate_and_convert_errors
637+
def list_namespaces_paginated(
638+
self, limit: Optional[int] = None, pagination_token: Optional[str] = None
639+
) -> ListNamespacesResponse:
640+
return self.namespace.list_paginated(limit=limit, pagination_token=pagination_token)

pinecone/db_data/index_asyncio.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -669,27 +669,19 @@ async def cancel_import(self, id: str):
669669
return await self.bulk_import.cancel(id=id)
670670

671671
@validate_and_convert_errors
672-
async def describe_namespace(
673-
self,
674-
namespace: str,
675-
**kwargs
676-
) -> "NamespaceDescription":
672+
async def describe_namespace(self, namespace: str) -> "NamespaceDescription":
677673
return await self.namespace.describe(namespace=namespace)
678674

679675
@validate_and_convert_errors
680-
async def delete_namespace(
681-
self,
682-
namespace: str,
683-
**kwargs
684-
) -> Dict[str, Any]:
676+
async def delete_namespace(self, namespace: str) -> Dict[str, Any]:
685677
return await self.namespace.delete(namespace=namespace)
686678

687679
@validate_and_convert_errors
688-
async def list_namespaces(
689-
self,
690-
limit: Optional[int] = None,
691-
pagination_token: Optional[str] = None,
692-
**kwargs
693-
) -> AsyncIterator["NamespaceDescription"]:
694-
async for namespace in self.namespace.list(limit=limit, pagination_token=pagination_token, **kwargs):
695-
yield namespace
680+
async def list_namespaces(self, **kwargs) -> AsyncIterator[ListNamespacesResponse]:
681+
return await self.namespace.list(**kwargs)
682+
683+
@validate_and_convert_errors
684+
async def list_namespaces_paginated(
685+
self, limit: Optional[int] = None, pagination_token: Optional[str] = None
686+
) -> ListNamespacesResponse:
687+
return await self.namespace.list_paginated(limit=limit, pagination_token=pagination_token)

pinecone/db_data/index_asyncio_interface.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SparseValues,
1212
SearchRecordsResponse,
1313
NamespaceDescription,
14+
ListNamespacesResponse,
1415
)
1516
from .query_results_aggregator import QueryNamespacesResults
1617
from .types import (
@@ -810,14 +811,12 @@ async def search_records(
810811
@abstractmethod
811812
async def describe_namespace(
812813
self,
813-
namespace: str,
814-
**kwargs
814+
namespace: str
815815
) -> NamespaceDescription:
816816
"""Describe a namespace within an index, showing the vector count within the namespace.
817817
818818
Args:
819819
namespace (str): The namespace to describe
820-
**kwargs: Additional arguments to pass to the API call
821820
822821
Returns:
823822
NamespaceDescription: Information about the namespace including vector count
@@ -827,14 +826,12 @@ async def describe_namespace(
827826
@abstractmethod
828827
async def delete_namespace(
829828
self,
830-
namespace: str,
831-
**kwargs
829+
namespace: str
832830
) -> Dict[str, Any]:
833831
"""Delete a namespace from an index.
834832
835833
Args:
836834
namespace (str): The namespace to delete
837-
**kwargs: Additional arguments to pass to the API call
838835
839836
Returns:
840837
Dict[str, Any]: Response from the delete operation
@@ -843,19 +840,44 @@ async def delete_namespace(
843840

844841
@abstractmethod
845842
async def list_namespaces(
846-
self,
847-
limit: Optional[int] = None,
848-
pagination_token: Optional[str] = None,
849-
**kwargs
850-
) -> AsyncIterator[NamespaceDescription]:
851-
"""Get a list of all namespaces within an index.
843+
self, **kwargs
844+
) -> AsyncIterator[ListNamespacesResponse]:
845+
"""List all namespaces in an index. This method automatically handles pagination to return all results.
852846
853847
Args:
854-
limit (Optional[int]): Max number of namespaces to return per page
855-
pagination_token (Optional[str]): Token to continue a previous listing operation
856-
**kwargs: Additional arguments to pass to the API call
848+
limit (Optional[int]): The maximum number of namespaces to return. If unspecified, the server will use a default value. [optional]
857849
858850
Returns:
859-
AsyncIterator[NamespaceDescription]: Async generator yielding namespace descriptions
851+
`ListNamespacesResponse`: Object containing the list of namespaces.
852+
853+
Examples:
854+
>>> async for namespace in index.list_namespaces(limit=5):
855+
... print(f"Namespace: {namespace.name}, Vector count: {namespace.vector_count}")
856+
Namespace: namespace1, Vector count: 1000
857+
Namespace: namespace2, Vector count: 2000
860858
"""
861859
pass
860+
861+
@abstractmethod
862+
async def list_namespaces_paginated(
863+
self, limit: Optional[int] = None, pagination_token: Optional[str] = None
864+
) -> ListNamespacesResponse:
865+
"""List all namespaces in an index with pagination support. The response includes pagination information if there are more results available.
866+
867+
Consider using the `list_namespaces` method to avoid having to handle pagination tokens manually.
868+
869+
Args:
870+
limit (Optional[int]): The maximum number of namespaces to return. If unspecified, the server will use a default value. [optional]
871+
pagination_token (Optional[str]): A token needed to fetch the next page of results. This token is returned
872+
in the response if additional results are available. [optional]
873+
874+
Returns:
875+
`ListNamespacesResponse`: Object containing the list of namespaces and pagination information.
876+
877+
Examples:
878+
>>> results = await index.list_namespaces_paginated(limit=5)
879+
>>> results.pagination.next
880+
eyJza2lwX3Bhc3QiOiI5OTMiLCJwcmVmaXgiOiI5OSJ9
881+
>>> next_results = await index.list_namespaces_paginated(limit=5, pagination_token=results.pagination.next)
882+
"""
883+
pass

pinecone/db_data/interfaces.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SparseValues,
1212
SearchRecordsResponse,
1313
NamespaceDescription,
14+
ListNamespacesResponse,
1415
)
1516
from .query_results_aggregator import QueryNamespacesResults
1617
from multiprocessing.pool import ApplyResult
@@ -682,9 +683,7 @@ def list(self, **kwargs):
682683
pass
683684

684685
@abstractmethod
685-
def describe_namespace(
686-
self, namespace: str, **kwargs
687-
) -> NamespaceDescription:
686+
def describe_namespace(self, namespace: str) -> NamespaceDescription:
688687
"""Describe a namespace within an index, showing the vector count within the namespace.
689688
690689
Args:
@@ -697,9 +696,7 @@ def describe_namespace(
697696
pass
698697

699698
@abstractmethod
700-
def delete_namespace(
701-
self, namespace: str, **kwargs
702-
) -> Dict[str, Any]:
699+
def delete_namespace(self, namespace: str) -> Dict[str, Any]:
703700
"""Delete a namespace from an index.
704701
705702
Args:
@@ -712,20 +709,44 @@ def delete_namespace(
712709
pass
713710

714711
@abstractmethod
715-
def list_namespaces(
716-
self,
717-
limit: Optional[int] = None,
718-
pagination_token: Optional[str] = None,
719-
**kwargs
720-
) -> Iterator[NamespaceDescription]:
721-
"""Get a list of all namespaces within an index.
712+
def list_namespaces(self, **kwargs) -> Iterator[ListNamespacesResponse]:
713+
"""List all namespaces in an index. This method automatically handles pagination to return all results.
722714
723715
Args:
724-
limit (Optional[int]): Max number of namespaces to return per page
725-
pagination_token (Optional[str]): Token to continue a previous listing operation
726-
**kwargs: Additional arguments to pass to the API call
716+
limit (Optional[int]): The maximum number of namespaces to return. If unspecified, the server will use a default value. [optional]
727717
728718
Returns:
729-
Iterator[NamespaceDescription]: Generator yielding namespace descriptions
719+
`ListNamespacesResponse`: Object containing the list of namespaces.
720+
721+
Examples:
722+
>>> results = list(index.list_namespaces(limit=5))
723+
>>> for namespace in results:
724+
... print(f"Namespace: {namespace.name}, Vector count: {namespace.vector_count}")
725+
Namespace: namespace1, Vector count: 1000
726+
Namespace: namespace2, Vector count: 2000
730727
"""
731728
pass
729+
730+
@abstractmethod
731+
def list_namespaces_paginated(
732+
self, limit: Optional[int] = None, pagination_token: Optional[str] = None
733+
) -> ListNamespacesResponse:
734+
"""List all namespaces in an index with pagination support. The response includes pagination information if there are more results available.
735+
736+
Consider using the `list_namespaces` method to avoid having to handle pagination tokens manually.
737+
738+
Args:
739+
limit (Optional[int]): The maximum number of namespaces to return. If unspecified, the server will use a default value. [optional]
740+
pagination_token (Optional[str]): A token needed to fetch the next page of results. This token is returned
741+
in the response if additional results are available. [optional]
742+
743+
Returns:
744+
`ListNamespacesResponse`: Object containing the list of namespaces and pagination information.
745+
746+
Examples:
747+
>>> results = index.list_namespaces_paginated(limit=5)
748+
>>> results.pagination.next
749+
eyJza2lwX3Bhc3QiOiI5OTMiLCJwcmVmaXgiOiI5OSJ9
750+
>>> next_results = index.list_namespaces_paginated(limit=5, pagination_token=results.pagination.next)
751+
"""
752+
pass

pinecone/db_data/resources/asyncio/namespace_asyncio.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
class NamespaceResourceAsyncio:
18-
def __init__(self, api_client, **kwargs) -> None:
18+
def __init__(self, api_client) -> None:
1919
self.__namespace_operations_api = AsyncioNamespaceOperationsApi(api_client)
2020

2121
async def describe(self, namespace: str) -> NamespaceDescription:
@@ -41,7 +41,7 @@ async def delete(self, namespace: str):
4141
args = NamespaceRequestFactory.delete_namespace_args(namespace=namespace)
4242
return await self.__namespace_operations_api.delete_namespace(**args)
4343

44-
async def list(self, **kwargs) -> AsyncIterator[NamespaceDescription]:
44+
async def list(self, **kwargs) -> AsyncIterator[ListNamespacesResponse]:
4545
"""
4646
Args:
4747
limit (Optional[int]): The maximum number of namespaces to fetch in each network call. If unspecified, the server will use a default value. [optional]
@@ -50,22 +50,55 @@ async def list(self, **kwargs) -> AsyncIterator[NamespaceDescription]:
5050
5151
Returns:
5252
Returns an async generator that yields each namespace. It automatically handles pagination tokens on your behalf so you can
53-
easily iterate over all results.
53+
easily iterate over all results. The `list` method accepts all of the same arguments as list_paginated
5454
5555
```python
5656
async for namespace in index.list_namespaces():
5757
print(namespace)
5858
```
59+
60+
You can convert the generator into a list by using an async list comprehension:
61+
62+
```python
63+
namespaces = [namespace async for namespace in index.list_namespaces()]
64+
```
65+
66+
You should be cautious with this approach because it will fetch all namespaces at once, which could be a large number
67+
of network calls and a lot of memory to hold the results.
5968
"""
6069
done = False
6170
while not done:
62-
args_dict = NamespaceRequestFactory.list_namespaces_args(**kwargs)
63-
results = await self.__namespace_operations_api.list_namespaces_operation(**args_dict)
64-
if len(results.namespaces) > 0:
71+
results = await self.list_paginated(**kwargs)
72+
if results.namespaces is not None and len(results.namespaces) > 0:
6573
for namespace in results.namespaces:
6674
yield namespace
6775

68-
if results.pagination:
76+
if results.pagination and results.pagination.next:
6977
kwargs.update({"pagination_token": results.pagination.next})
7078
else:
71-
done = True
79+
done = True
80+
81+
async def list_paginated(
82+
self, limit: Optional[int] = None, pagination_token: Optional[str] = None
83+
) -> ListNamespacesResponse:
84+
"""
85+
Args:
86+
limit (Optional[int]): The maximum number of namespaces to return. If unspecified, the server will use a default value. [optional]
87+
pagination_token (Optional[str]): A token needed to fetch the next page of results. This token is returned
88+
in the response if additional results are available. [optional]
89+
90+
Returns:
91+
`ListNamespacesResponse`: Object containing the list of namespaces and pagination information.
92+
93+
List all namespaces in an index with pagination support. The response includes pagination information if there are more results available.
94+
95+
Consider using the `list` method to avoid having to handle pagination tokens manually.
96+
97+
Examples:
98+
>>> results = await index.list_paginated(limit=5)
99+
>>> results.pagination.next
100+
eyJza2lwX3Bhc3QiOiI5OTMiLCJwcmVmaXgiOiI5OSJ9
101+
>>> next_results = await index.list_paginated(limit=5, pagination_token=results.pagination.next)
102+
"""
103+
args = NamespaceRequestFactory.list_namespaces_args(limit=limit, pagination_token=pagination_token)
104+
return await self.__namespace_operations_api.list_namespaces_operation(**args)

0 commit comments

Comments
 (0)