Create a collection (Python)

Creates a new collection in a database.

Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart.

Result

Creates a collection with the specified parameters.

Returns a Collection object. You can use this object to work with documents in the collection.

Unless you specify the document_type parameter, the collection is typed as Collection[dict]. For more information, see Typing support.

Parameters

You cannot edit a collection’s definition after you create the collection.

The signature of this method changed in Python client version 2.0.

If you are using an earlier version, DataStax recommends upgrading to the latest version. For more information, see Data API client upgrade guide (Python).

Use the create_collection method, which belongs to the astrapy.Database class.

Method signature
create_collection(
  name: str,
  *,
  definition: CollectionDefinition | dict[str, Any] | None,
  document_type: type[Any],
  keyspace: str,
  collection_admin_timeout_ms: int,
  embedding_api_key: str | EmbeddingHeadersProvider,
  spawn_api_options: APIOptions,
) -> Collection

Most astrapy objects have an asynchronous counterpart for use within the asyncio framework. To get an AsyncCollection, use the create_collection method of instances of AsyncDatabase, or use the to_async method of the synchronous Collection class. For more information, see AsyncCollection.

Name Type Summary

name

str

The name of the new collection.

Collection names must follow these rules:

  • Can contain letters, numbers, and underscores

  • Cannot exceed 48 characters

  • Must be unique within the keyspace

definition

CollectionDefinition

Optional. The full configuration for the collection. See Properties of CollectionDefinition and Examples for more details.

document_type

type

Optional. A formal specifier for the type checker. If provided, document_type must match the type hint specified in the assignment. For more information, see Typing support.

Default: Collection[dict]

keyspace

str

Optional. The keyspace in which to create the collection.

For an example, see Create a collection and specify the keyspace.

Default: The working keyspace for the database. This is default_keyspace unless you set a different working keyspace when you created the Database object.

collection_admin_timeout_ms

int

Optional. A timeout, in milliseconds, to impose on the underlying API request. If not provided, the corresponding Database defaults apply.

embedding_api_key

str | EmbeddingHeadersProvider

Optional. This only applies to collections with a vectorize embedding provider integration.

Use this option to provide the embedding provider API key directly with headers instead of using an API key in the Astra DB KMS.

The API key is sent to the Data API for every operation on the collection. It is useful when a vectorize integration is configured but no credentials are stored, or when you want to override the stored credentials. For more information, see Manage embedding provider integrations for vectorize.

If you use an AWS embedding provider, the embedding_api_key argument must instead use the AWSEmbeddingHeadersProvider class to pass your access ID and secret ID.

spawn_api_options

APIOptions

Optional. A complete or partial specification of the APIOptions to override the defaults inherited from the Database. Use this to customize the interaction of the Python client with the collection. For example, you can change the serialization/deserialization options or default timeouts.

If APIOptions is passed together with a named parameter such as a timeout, the latter takes precedence over the corresponding spawn_api_options setting.

Properties of CollectionDefinition
Name Type Summary

vector

CollectionVectorOptions

Optional. The vector configuration for the collection. This includes things like the vector dimension, similarity metric, and source model. This also includes settings for server-side embedding generation if you want your collection to have vectorize enabled.

Required for vector search and hybrid search.

The CollectionVectorOptions class has the following attributes:

  • dimension (int): The dimension for vector embeddings in the collection. This should match the dimension of the vector that your embedding model produces. Optional if you specify a vector.service.modelName value that has a default dimension value.

  • metric (string): Optional. The similarity metric to use for vector search. Can be one of the values in astrapy.constants.VectorMetric: COSINE, DOT_PRODUCT, EUCLIDEAN.

  • sourceModel (string): Optional. The model used to generate the vector embeddings. This enables certain vector optimizations on the index. Can be one of: ada002, bert, cohere-v3, gecko, nv-qa-4, openai-v3-large, openai-v3-small, other.

  • service (object): Optional. The configuration for a vectorize embedding provider integration. This lets your collection use vectorize to automatically generate embeddings. See Create a collection that can automatically generate vector embeddings, use findEmbeddingProviders, or see the documentation for your embedding provider integration to determine what values to specify.

lexical

CollectionLexicalOptions

Optional. The lexical search configuration for the collection.

Only collections in databases in the AWS us-east-2 region support this parameter.

The CollectionLexicalOptions object has the following properties:

Default: A CollectionLexicalOptions object with an enabled value of True and an analyzer value of "standard", which corresponds to the standard Apache Lucene™ analyzer.

rerank

CollectionRerankOptions

Optional. The reranker configuration for the collection.

Only collections in databases in the AWS us-east-2 region support this parameter.

The CollectionRerankOptions object has the following properties:

Default: A RerankServiceOptions object with an enabled value of True and a service value corresponding to the NVIDIA llama-3.2-nv-rerankqa-1b-v2 reranking model. This means that reranking is enabled by default.

indexing

dict

Optional. The selective indexing configuration for the collection.

You must use & to escape any . or & in field names in the indexing clause. You cannot use & to escape any other characters. Dot notation, which is used to reference nested fields, should not be escaped. For more information, see Work with . and & in field names (Python).

Default: All fields of all documents.

default_id

CollectionDefaultIDOptions

Optional. Specifies the default ID type for documents in the collection. This is used when you insert a document without an _id field.

Can be one of:

  • CollectionDefaultIDOptions(DefaultIdType.OBJECTID): Each autogenerated _id value is an objectId as provided by the bson library.

  • CollectionDefaultIDOptions(DefaultIdType.UUIDV7): Each autogenerated _id value is a version 7 UUID. This is designed as a replacement for version 1 time UUID, and it is recommended for use in new systems.

  • CollectionDefaultIDOptions(DefaultIdType.UUIDV6): Each autogenerated _id value is a version 6 UUID. This is field-compatible with version 1 time UUIDs, and it supports lexicographical sorting.

  • CollectionDefaultIDOptions(DefaultIdType.UUID): Each autogenerated _id value is a version 4 UUID. This type is analogous to the uuid type and functions in Apache Cassandra®.

  • CollectionDefaultIDOptions(DefaultIdType.DEFAULT): Each autogenerated _id value is a string form of a version 4 UUID.

For more information, see Document IDs (Python).

Default: CollectionDefaultIDOptions(DefaultIdType.DEFAULT)

Examples

The following examples demonstrate how to create a collection.

Create a collection that is not vector-enabled

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

from astrapy import DataAPIClient

# Get a database
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Create a collection
collection = database.create_collection("COLLECTION_NAME")

Create a collection that can store vector embeddings

Collections that are vector-enabled can store vector embeddings in the reserved $vector field and work with vector search.

For optimal vector search results, you should specify the dimension, metric, and source model of your vector embeddings. All vector embeddings in a collection should be generated by the same model with the same dimensions. The source model can be one of: ada002, bert, cohere-v3, gecko, nv-qa-4, openai-v3-large, openai-v3-small, other.

The Python client supports multiple ways to create a collection:

  • You can define the collection parameters in a CollectionDefinition object and then create the collection from the CollectionDefinition object.

  • You can use a fluent interface to build the collection definition and then create the collection from the definition.

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import CollectionDefinition, CollectionVectorOptions

# Get an existing database
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Create a collection
collection_definition = CollectionDefinition(
    vector=CollectionVectorOptions(
        dimension=1024, metric=VectorMetric.COSINE, source_model="nv-qa-4"
    ),
)
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import CollectionDefinition

# Get an existing database
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

collection_definition = (
    CollectionDefinition.builder()
    .with_vector_dimension(1024)
    .with_vector_metric(VectorMetric.COSINE)
    .with_vector_source_model("nv-qa-4")
    .build()
)

collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

Create a collection that can automatically generate vector embeddings

If you want to automatically generate vector embeddings, create a vector-enabled collection and configure an embedding provider integration for the collection.

The configuration depends on the embedding provider.

Configure Azure OpenAI as the embedding provider

For more detailed instructions, see Integrate Azure OpenAI as an embedding provider.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionVectorOptions,
    VectorServiceOptions,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = CollectionDefinition(
    vector=CollectionVectorOptions(
        metric=VectorMetric.SIMILARITY_METRIC,
        dimension=MODEL_DIMENSIONS,
        service=VectorServiceOptions(
            provider="azureOpenAI",
            model_name="MODEL_NAME",
            authentication={
                "providerKey": "API_KEY_NAME",
            },
            parameters={
                "resourceName": "RESOURCE_NAME",
                "deploymentId": "DEPLOYMENT_ID",
            },
        ),
    )
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = (
    CollectionDefinition.builder()
    .with_vector_dimension(MODEL_DIMENSIONS)
    .with_vector_metric(VectorMetric.SIMILARITY_METRIC)
    .with_vector_service(
        provider="azureOpenAI",
        model_name="MODEL_NAME",
        authentication={
            "providerKey": "API_KEY_NAME",
        },
        parameters={
            "resourceName": "RESOURCE_NAME",
            "deploymentId": "DEPLOYMENT_ID",
        },
    )
    .build()
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")

Replace the following:

  • COLLECTION_NAME: The name for your collection.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean.

  • API_KEY_NAME: The name of the Azure OpenAI API key that you want to use. Must be the name of an existing Azure OpenAI API key in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embedding_api_key parameter when you instantiate a Collection object with the commands to create a collection or get a collection. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: text-embedding-3-small, text-embedding-3-large, text-embedding-ada-002.

    For Azure OpenAI, you must select the model that matches the one deployed to your DEPLOYMENT_ID in Azure.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

  • RESOURCE_NAME: The name of your Azure OpenAI Service resource, as defined in the resource’s Instance details. For more information, see the Azure OpenAI documentation.

  • DEPLOYMENT_ID: Your Azure OpenAI resource’s Deployment name. For more information, see the Azure OpenAI documentation.

Configure Hugging Face (Dedicated) as the embedding provider

For more detailed instructions, see Integrate Hugging Face Dedicated as an embedding provider.

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionVectorOptions,
    VectorServiceOptions,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = CollectionDefinition(
    vector=CollectionVectorOptions(
        metric=VectorMetric.SIMILARITY_METRIC,
        dimension=MODEL_DIMENSIONS,
        service=VectorServiceOptions(
            provider="huggingfaceDedicated",
            model_name="MODEL_NAME",
            authentication={
                "providerKey": "API_KEY_NAME",
            },
            parameters={
                "endpointName": "ENDPOINT_NAME",
                "regionName": "REGION",
                "cloudName": "CLOUD_PROVIDER",
            },
        ),
    )
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = (
    CollectionDefinition.builder()
    .with_vector_dimension(MODEL_DIMENSIONS)
    .with_vector_metric(VectorMetric.SIMILARITY_METRIC)
    .with_vector_service(
        provider="huggingfaceDedicated",
        model_name="MODEL_NAME",
        authentication={
            "providerKey": "API_KEY_NAME",
        },
        parameters={
            "endpointName": "ENDPOINT_NAME",
            "regionName": "REGION",
            "cloudName": "CLOUD_PROVIDER",
        },
    )
    .build()
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")

Replace the following:

  • COLLECTION_NAME: The name for your collection.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean.

  • API_KEY_NAME: The name of the Hugging Face Dedicated user access token that you want to use. Must be the name of an existing Hugging Face Dedicated user access token in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embedding_api_key parameter when you instantiate a Collection object with the commands to create a collection or get a collection. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: endpoint-defined-model.

    For Hugging Face Dedicated, you must deploy the model as a text embeddings inference (TEI) container.

    You must set MODEL_NAME to endpoint-defined-model because this integration uses the model specified in your dedicated endpoint configuration.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

  • ENDPOINT_NAME: The programmatically-generated name of your Hugging Face Dedicated endpoint. This is the first part of the endpoint URL. For example, if your endpoint URL is https://mtp1x7muf6qyn3yh.us-east-2.aws.endpoints.huggingface.cloud, the endpoint name is mtp1x7muf6qyn3yh.

  • REGION: The cloud provider region your Hugging Face Dedicated endpoint is deployed to. For example, us-east-2.

  • CLOUD_PROVIDER: The cloud provider your Hugging Face Dedicated endpoint is deployed to. For example, aws.

Configure Hugging Face (Serverless) as the embedding provider

For more detailed instructions, see Integrate Hugging Face Serverless as an embedding provider.

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionVectorOptions,
    VectorServiceOptions,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = CollectionDefinition(
    vector=CollectionVectorOptions(
        metric=VectorMetric.SIMILARITY_METRIC,
        dimension=MODEL_DIMENSIONS,
        service=VectorServiceOptions(
            provider="huggingface",
            model_name="MODEL_NAME",
            authentication={
                "providerKey": "API_KEY_NAME",
            },
        ),
    )
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionVectorOptions,
    VectorServiceOptions,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = (
    CollectionDefinition.builder()
    .with_vector_dimension(MODEL_DIMENSIONS)
    .with_vector_metric(VectorMetric.SIMILARITY_METRIC)
    .with_vector_service(
        provider="huggingface",
        model_name="MODEL_NAME",
        authentication={
            "providerKey": "API_KEY_NAME",
        },
    )
    .build()
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")

Replace the following:

  • COLLECTION_NAME: The name for your collection.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean.

  • API_KEY_NAME: The name of the Hugging Face Serverless user access token that you want to use. Must be the name of an existing Hugging Face Serverless user access token in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embedding_api_key parameter when you instantiate a Collection object with the commands to create a collection or get a collection. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: sentence-transformers/all-MiniLM-L6-v2, intfloat/multilingual-e5-large, intfloat/multilingual-e5-large-instruct, BAAI/bge-small-en-v1.5, BAAI/bge-base-en-v1.5, BAAI/bge-large-en-v1.5.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

Configure Jina AI as the embedding provider

For more detailed instructions, see Integrate Jina AI as an embedding provider.

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionVectorOptions,
    VectorServiceOptions,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = CollectionDefinition(
    vector=CollectionVectorOptions(
        metric=VectorMetric.SIMILARITY_METRIC,
        dimension=MODEL_DIMENSIONS,
        service=VectorServiceOptions(
            provider="jinaAI",
            model_name="MODEL_NAME",
            authentication={
                "providerKey": "API_KEY_NAME",
            },
        ),
    )
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionVectorOptions,
    VectorServiceOptions,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = (
    CollectionDefinition.builder()
    .with_vector_dimension(MODEL_DIMENSIONS)
    .with_vector_metric(VectorMetric.SIMILARITY_METRIC)
    .with_vector_service(
        provider="jinaAI",
        model_name="MODEL_NAME",
        authentication={
            "providerKey": "API_KEY_NAME",
        },
    )
    .build()
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")

Replace the following:

  • COLLECTION_NAME: The name for your collection.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean.

  • API_KEY_NAME: The name of the Jina AI API key that you want to use. Must be the name of an existing Jina AI API key in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embedding_api_key parameter when you instantiate a Collection object with the commands to create a collection or get a collection. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: jina-embeddings-v2-base-en, jina-embeddings-v2-base-de, jina-embeddings-v2-base-es, jina-embeddings-v2-base-code, jina-embeddings-v2-base-zh.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

Configure Mistral AI as the embedding provider

For more detailed instructions, see Integrate Mistral AI as an embedding provider.

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionVectorOptions,
    VectorServiceOptions,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = CollectionDefinition(
    vector=CollectionVectorOptions(
        metric=VectorMetric.SIMILARITY_METRIC,
        dimension=MODEL_DIMENSIONS,
        service=VectorServiceOptions(
            provider="mistral",
            model_name="MODEL_NAME",
            authentication={
                "providerKey": "API_KEY_NAME",
            },
        ),
    )
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionVectorOptions,
    VectorServiceOptions,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = (
    CollectionDefinition.builder()
    .with_vector_dimension(MODEL_DIMENSIONS)
    .with_vector_metric(VectorMetric.SIMILARITY_METRIC)
    .with_vector_service(
        provider="mistral",
        model_name="MODEL_NAME",
        authentication={
            "providerKey": "API_KEY_NAME",
        },
    )
    .build()
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")

Replace the following:

  • COLLECTION_NAME: The name for your collection.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean.

  • API_KEY_NAME: The name of the Mistral AI API key that you want to use. Must be the name of an existing Mistral AI API key in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embedding_api_key parameter when you instantiate a Collection object with the commands to create a collection or get a collection. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: mistral-embed.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

Configure NVIDIA as the embedding provider

For more detailed instructions, see Integrate NVIDIA as an embedding provider. Your database must be in a supported region.

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionVectorOptions,
    VectorServiceOptions,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = CollectionDefinition(
    vector=CollectionVectorOptions(
        metric=VectorMetric.COSINE,
        service=VectorServiceOptions(
            provider="nvidia",
            model_name="nvidia/nv-embedqa-e5-v5",
        ),
    )
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = (
    CollectionDefinition.builder()
    .with_vector_metric(VectorMetric.COSINE)
    .with_vector_service(
        provider="nvidia", model_name="nvidia/nv-embedqa-e5-v5"
    )
    .build()
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")

Configure OpenAI as the embedding provider

For more detailed instructions, see Integrate OpenAI as an embedding provider.

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionVectorOptions,
    VectorServiceOptions,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = CollectionDefinition(
    vector=CollectionVectorOptions(
        metric=VectorMetric.SIMILARITY_METRIC,
        dimension=MODEL_DIMENSIONS,
        service=VectorServiceOptions(
            provider="openai",
            model_name="MODEL_NAME",
            authentication={
                "providerKey": "API_KEY_NAME",
            },
            parameters={
                "organizationId": "ORGANIZATION_ID",
                "projectId": "PROJECT_ID",
            },
        ),
    )
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = (
    CollectionDefinition.builder()
    .with_vector_dimension(MODEL_DIMENSIONS)
    .with_vector_metric(VectorMetric.SIMILARITY_METRIC)
    .with_vector_service(
        provider="openai",
        model_name="MODEL_NAME",
        authentication={
            "providerKey": "API_KEY_NAME",
        },
        parameters={
            "organizationId": "ORGANIZATION_ID",
            "projectId": "PROJECT_ID",
        },
    )
    .build()
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")

Replace the following:

  • COLLECTION_NAME: The name for your collection.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean.

  • API_KEY_NAME: The name of the OpenAI API key that you want to use. Must be the name of an existing OpenAI API key in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embedding_api_key parameter when you instantiate a Collection object with the commands to create a collection or get a collection. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: text-embedding-3-small, text-embedding-3-large, text-embedding-ada-002.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

  • ORGANIZATION_ID: Optional. The ID of the OpenAI organization that owns the API key. Only required if your OpenAI account belongs to multiple organizations or if you are using a legacy user API key to access projects. For more information about organization IDs, see the OpenAI API reference.

  • PROJECT_ID: Optional. The ID of the OpenAI project that owns the API key. This cannot use the default project. Only required if your OpenAI account belongs to multiple organizations or if you are using a legacy user API key to access projects. For more information about project IDs, see the OpenAI API reference.

Configure Upstage as the embedding provider

For more detailed instructions, see Integrate Upstage as an embedding provider.

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionVectorOptions,
    VectorServiceOptions,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = CollectionDefinition(
    vector=CollectionVectorOptions(
        metric=VectorMetric.SIMILARITY_METRIC,
        dimension=MODEL_DIMENSIONS,
        service=VectorServiceOptions(
            provider="upstageAI",
            model_name="MODEL_NAME",
            authentication={
                "providerKey": "API_KEY_NAME",
            },
        ),
    )
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionVectorOptions,
    VectorServiceOptions,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = (
    CollectionDefinition.builder()
    .with_vector_dimension(MODEL_DIMENSIONS)
    .with_vector_metric(VectorMetric.SIMILARITY_METRIC)
    .with_vector_service(
        provider="upstageAI",
        model_name="MODEL_NAME",
        authentication={
            "providerKey": "API_KEY_NAME",
        },
    )
    .build()
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")

Replace the following:

  • COLLECTION_NAME: The name for your collection.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean.

  • API_KEY_NAME: The name of the Upstage API key that you want to use. Must be the name of an existing Upstage API key in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embedding_api_key parameter when you instantiate a Collection object with the commands to create a collection or get a collection. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: solar-embedding-1-large.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

Configure Voyage AI as the embedding provider

For more detailed instructions, see Integrate Voyage AI as an embedding provider.

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionVectorOptions,
    VectorServiceOptions,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = CollectionDefinition(
    vector=CollectionVectorOptions(
        metric=VectorMetric.SIMILARITY_METRIC,
        dimension=MODEL_DIMENSIONS,
        service=VectorServiceOptions(
            provider="voyageAI",
            model_name="MODEL_NAME",
            authentication={
                "providerKey": "API_KEY_NAME",
            },
        ),
    )
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionVectorOptions,
    VectorServiceOptions,
)

# Instantiate the client
client = DataAPIClient()

# Connect to a database
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Define the collection
collection_definition = (
    CollectionDefinition.builder()
    .with_vector_dimension(MODEL_DIMENSIONS)
    .with_vector_metric(VectorMetric.SIMILARITY_METRIC)
    .with_vector_service(
        provider="voyageAI",
        model_name="MODEL_NAME",
        authentication={
            "providerKey": "API_KEY_NAME",
        },
    )
    .build()
)

# Create the collection
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

print(f"* Collection: {collection.full_name}\n")

Replace the following:

  • COLLECTION_NAME: The name for your collection.

  • SIMILARITY_METRIC: The method you want to use to calculate vector similarity scores. The available metrics are Cosine (default), Dot Product, and Euclidean.

  • API_KEY_NAME: The name of the Voyage AI API key that you want to use. Must be the name of an existing Voyage AI API key in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embedding_api_key parameter when you instantiate a Collection object with the commands to create a collection or get a collection. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: voyage-2, voyage-code-2, voyage-finance-2, voyage-large-2, voyage-large-2-instruct, voyage-law-2, voyage-multilingual-2.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

Create a collection that supports hybrid search

If you want to perform hybrid search on your collection, you must create a collection that has vector, lexical, and rerank enabled. Your collection must also be in a database in the AWS us-east-2 region.

Lexical and rerank are enabled by default when you create a collection in a database in the AWS us-east-2 region, but you can optionally configure the lexical analyzer and the reranker model.

For configuration details about the lexical analyzer, see Find data with CQL analyzers. The following example uses a configuration suitable for English text.

For configuration details about the reranker model, inspect the available reranker models. Only the NVIDIA llama-3.2-nv-rerankqa-1b-v2 reranking model reranker model is supported.

The Python client supports multiple ways to create a collection:

  • You can define the collection parameters in a CollectionDefinition object and then create the collection from the CollectionDefinition object.

  • You can use a fluent interface to build the collection definition and then create the collection from the definition.

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import (
    CollectionDefinition,
    CollectionLexicalOptions,
    CollectionRerankOptions,
    CollectionVectorOptions,
    RerankServiceOptions,
    VectorServiceOptions,
)

# Get an existing database
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Create a collection
collection_definition = CollectionDefinition(
    vector=CollectionVectorOptions(
        metric=VectorMetric.COSINE,
        dimension=1024,
        service=VectorServiceOptions(
            provider="nvidia",
            model_name="nvidia/nv-embedqa-e5-v5",
        ),
    ),
    lexical=CollectionLexicalOptions(
        analyzer={
            "tokenizer": {"name": "standard", "args": {}},
            "filters": [
                {"name": "lowercase"},
                {"name": "stop"},
                {"name": "porterstem"},
                {"name": "asciifolding"},
            ],
            "charFilters": [],
        },
        enabled=True,
    ),
    rerank=CollectionRerankOptions(
        enabled=True,
        service=RerankServiceOptions(
            provider="nvidia",
            model_name="nvidia/llama-3.2-nv-rerankqa-1b-v2",
        ),
    ),
)
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)
from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import CollectionDefinition

# Get an existing database
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Create a collection
collection_definition = (
    CollectionDefinition.builder()
    .with_vector_dimension(1024)
    .with_vector_metric(VectorMetric.COSINE)
    .with_vector_service(
        provider="nvidia",
        model_name="nvidia/nv-embedqa-e5-v5",
    )
    .with_lexical(
        {
            "tokenizer": {"name": "standard", "args": {}},
            "filters": [
                {"name": "lowercase"},
                {"name": "stop"},
                {"name": "porterstem"},
                {"name": "asciifolding"},
            ],
            "charFilters": [],
        }
    )
    .with_rerank("nvidia", "nvidia/llama-3.2-nv-rerankqa-1b-v2")
    .build()
)
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

Create a collection that supports lexicographical matching

If you want to use lexicographical matching to find documents in your collection, you must create a collection that has lexical enabled. Your collection must also be in a database in the AWS us-east-2 region.

Lexical is enabled by default when you create a collection in a database in the AWS us-east-2 region, but you can optionally configure the lexical analyzer.

For configuration details about the lexical analyzer, see Find data with CQL analyzers. The following example uses a configuration suitable for English text.

The Python client supports multiple ways to create a collection:

  • You can define the collection parameters in a CollectionDefinition object and then create the collection from the CollectionDefinition object.

  • You can use a fluent interface to build the collection definition and then create the collection from the definition.

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.info import (
    CollectionDefinition,
    CollectionLexicalOptions,
)

# Get an existing database
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Create a collection
collection_definition = CollectionDefinition(
    lexical=CollectionLexicalOptions(
        analyzer={
            "tokenizer": {"name": "standard", "args": {}},
            "filters": [
                {"name": "lowercase"},
                {"name": "stop"},
                {"name": "porterstem"},
                {"name": "asciifolding"},
            ],
            "charFilters": [],
        },
        enabled=True,
    ),
)
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)
from astrapy import DataAPIClient
from astrapy.info import CollectionDefinition

# Get an existing database
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Create a collection
collection_definition = (
    CollectionDefinition.builder()
    .with_lexical(
        {
            "tokenizer": {"name": "standard", "args": {}},
            "filters": [
                {"name": "lowercase"},
                {"name": "stop"},
                {"name": "porterstem"},
                {"name": "asciifolding"},
            ],
            "charFilters": [],
        }
    )
    .build()
)
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

Create a collection and specify the default ID format

For more information about the default ID format, see Document IDs (Python). For allowed values, see the Parameters.

The Python client supports multiple ways to create a collection:

  • You can define the collection parameters in a CollectionDefinition object and then create the collection from the CollectionDefinition object.

  • You can use a fluent interface to build the collection definition and then create the collection from the definition.

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.constants import DefaultIdType
from astrapy.info import (
    CollectionDefaultIDOptions,
    CollectionDefinition,
)

# Get an existing database
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Create a collection
collection_definition = CollectionDefinition(
    default_id=CollectionDefaultIDOptions(DefaultIdType.OBJECTID),
)
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)
from astrapy import DataAPIClient
from astrapy.constants import DefaultIdType
from astrapy.info import CollectionDefinition

# Get an existing database
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Create a collection
collection_definition = (
    CollectionDefinition.builder()
    .with_default_id(DefaultIdType.OBJECTID)
    .build()
)
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

Create a collection and specify which fields to index

For more information about selective indexing, see Indexes in collections (Python).

The Python client supports multiple ways to create a collection:

  • You can define the collection parameters in a CollectionDefinition object and then create the collection from the CollectionDefinition object.

  • You can use a fluent interface to build the collection definition and then create the collection from the definition.

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.info import CollectionDefinition

# Get an existing database
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Create a collection
collection_definition = CollectionDefinition(
    indexing={"allow": ["city", "country"]},
)
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)
from astrapy import DataAPIClient
from astrapy.info import CollectionDefinition

# Get an existing database
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Create a collection
collection_definition = (
    CollectionDefinition.builder()
    .with_indexing("allow", ["city", "country"])
    .build()
)
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

Create a collection and specify which fields shouldn’t be indexed

For more information about selective indexing, see Indexes in collections (Python).

The Python client supports multiple ways to create a collection:

  • You can define the collection parameters in a CollectionDefinition object and then create the collection from the CollectionDefinition object.

  • You can use a fluent interface to build the collection definition and then create the collection from the definition.

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

  • CollectionDefinition object

  • Fluent interface

from astrapy import DataAPIClient
from astrapy.info import CollectionDefinition

# Get an existing database
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Create a collection
collection_definition = CollectionDefinition(
    indexing={"deny": ["city", "country"]},
)
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)
from astrapy import DataAPIClient
from astrapy.info import CollectionDefinition

# Get an existing database
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Create a collection
collection_definition = (
    CollectionDefinition.builder()
    .with_indexing("deny", ["city", "country"])
    .build()
)
collection = database.create_collection(
    "COLLECTION_NAME",
    definition=collection_definition,
)

Create a collection and specify the keyspace

The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.

from astrapy import DataAPIClient

# Get a database
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT", token="APPLICATION_TOKEN"
)

# Create a collection
collection = database.create_collection(
    "COLLECTION_NAME", keyspace="KEYSPACE_NAME"
)

Client reference

For more information, see the client reference.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM