All Products
Search
Document Center

Object Storage Service:Create a vector index (Python SDK V2)

Last Updated:Jun 03, 2026

Call the PutVectorIndex operation using the Python SDK V2 to create a vector index in a vector bucket.

Permissions

An Alibaba Cloud account has all permissions by default. In contrast, a Resource Access Management (RAM) user or a RAM role does not have any permissions by default. The Alibaba Cloud account or an administrator must grant permissions for this operation using a RAM policy or a bucket policy.

API

Action

Description

PutVectorIndex

oss:PutVectorIndex

Creates a vector index.

Method definition

put_vector_index(request: PutVectorIndexRequest, **kwargs) → PutVectorIndexResult

Request parameters

Parameter

Type

Description

request

PutVectorIndexRequest

Set the request parameters, such as the name of the vector bucket. For more information, see PutVectorIndexRequest.

Return values

Type

Description

PutVectorIndexResult

The return value. For more information, see PutVectorIndexResult.

For the complete definition of the method, see put_vector_index.

Sample code

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors

parser = argparse.ArgumentParser(description="vector put vector index sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain name that other services can use to access OSS')
parser.add_argument('--index_name', help='The name of the vector index.', required=True)
parser.add_argument('--account_id', help='The account ID.', required=True)

def main():
    args = parser.parse_args()

    # Load credentials from environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Using the SDK's default configuration
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    cfg.account_id = args.account_id
    cfg.use_internal_endpoint = True  # To access the service over the public network, set this parameter to False or remove this line.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    vector_client = oss_vectors.Client(cfg)

    result = vector_client.put_vector_index(oss_vectors.models.PutVectorIndexRequest(
        bucket=args.bucket,
        index_name=args.index_name,
        dimension=512,
        data_type='float32',
        distance_metric='euclidean',
        metadata={"nonFilterableMetadataKeys": ["key1", "key2"]}
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
    )

if __name__ == "__main__":
    main()

References

For the complete sample code, see put_vector_index.py.