|
50 | 50 | DescribeNamespaceRequest, |
51 | 51 | DeleteNamespaceRequest, |
52 | 52 | ListNamespacesRequest, |
| 53 | + CreateNamespaceRequest, |
| 54 | + MetadataSchema, |
| 55 | + MetadataFieldProperties, |
53 | 56 | ) |
54 | 57 | from pinecone.core.grpc.protos.db_data_2025_10_pb2_grpc import VectorServiceStub |
55 | 58 | from pinecone import Vector, SparseValues |
@@ -769,6 +772,65 @@ def describe_index_stats( |
769 | 772 | json_response = json_format.MessageToDict(response) |
770 | 773 | return parse_stats_response(json_response) |
771 | 774 |
|
| 775 | + @require_kwargs |
| 776 | + def create_namespace( |
| 777 | + self, name: str, schema: Optional[Dict[str, Any]] = None, async_req: bool = False, **kwargs |
| 778 | + ) -> Union[NamespaceDescription, PineconeGrpcFuture]: |
| 779 | + """ |
| 780 | + The create_namespace operation creates a namespace in a serverless index. |
| 781 | +
|
| 782 | + Examples: |
| 783 | +
|
| 784 | + .. code-block:: python |
| 785 | +
|
| 786 | + >>> index.create_namespace(name='my_namespace') |
| 787 | +
|
| 788 | + >>> # Create namespace asynchronously |
| 789 | + >>> future = index.create_namespace(name='my_namespace', async_req=True) |
| 790 | + >>> namespace = future.result() |
| 791 | +
|
| 792 | + Args: |
| 793 | + name (str): The name of the namespace to create. |
| 794 | + schema (Optional[Dict[str, Any]]): Optional schema configuration for the namespace as a dictionary. [optional] |
| 795 | + async_req (bool): If True, the create_namespace operation will be performed asynchronously. [optional] |
| 796 | +
|
| 797 | + Returns: NamespaceDescription object which contains information about the created namespace, or a PineconeGrpcFuture object if async_req is True. |
| 798 | + """ |
| 799 | + timeout = kwargs.pop("timeout", None) |
| 800 | + |
| 801 | + # Build MetadataSchema from dict if provided |
| 802 | + metadata_schema = None |
| 803 | + if schema is not None: |
| 804 | + if isinstance(schema, dict): |
| 805 | + # Convert dict to MetadataSchema |
| 806 | + fields = {} |
| 807 | + for key, value in schema.get("fields", {}).items(): |
| 808 | + if isinstance(value, dict): |
| 809 | + filterable = value.get("filterable", False) |
| 810 | + fields[key] = MetadataFieldProperties(filterable=filterable) |
| 811 | + else: |
| 812 | + # If value is already a MetadataFieldProperties, use it directly |
| 813 | + fields[key] = value |
| 814 | + metadata_schema = MetadataSchema(fields=fields) |
| 815 | + else: |
| 816 | + # Assume it's already a MetadataSchema |
| 817 | + metadata_schema = schema |
| 818 | + |
| 819 | + request_kwargs: Dict[str, Any] = {"name": name} |
| 820 | + if metadata_schema is not None: |
| 821 | + request_kwargs["schema"] = metadata_schema |
| 822 | + |
| 823 | + request = CreateNamespaceRequest(**request_kwargs) |
| 824 | + |
| 825 | + if async_req: |
| 826 | + future = self.runner.run(self.stub.CreateNamespace.future, request, timeout=timeout) |
| 827 | + return PineconeGrpcFuture( |
| 828 | + future, timeout=timeout, result_transformer=parse_namespace_description |
| 829 | + ) |
| 830 | + |
| 831 | + response = self.runner.run(self.stub.CreateNamespace, request, timeout=timeout) |
| 832 | + return parse_namespace_description(response) |
| 833 | + |
772 | 834 | @require_kwargs |
773 | 835 | def describe_namespace(self, namespace: str, **kwargs) -> NamespaceDescription: |
774 | 836 | """ |
|
0 commit comments