---
question: How do I create a collection with the right dimension?
kind: task
via_web: na
via_cli: na
via_python: available
via_api: available
via_config: na
ref_python: ensure_collection
ref_api: PUT /api/admin/system/resources/vector_store/entry
prereq: You need a module. [Create a module](/capabilities/build/modules/creating-a-module) first.
next:
  - /capabilities/build/vector-search/add-embeddings
  - /capabilities/build/vector-search/search
---

# Create a collection

A collection has one dimension, the length of every vector in it, and one distance metric. A module creates the collections it needs when it starts. A super admin can also create one over the Nexus API by writing its first vector.

## Web UI

Not applicable. System › Data › Vector store adds vectors only inside a collection that already exists. Create the collection from the module (Python) or with the REST call on this page, then add vectors in Nexus: see [Add embeddings](/capabilities/build/vector-search/add-embeddings?via=web).

## CLI

Not applicable. There is no `abi` command for the vector store. Use the Python steps on this page. The command list is the [CLI reference](/user-guide/reference/cli).

## Python

### Declare the service

```python title="src/my_module/__init__.py"
from naas_abi_sdk import BaseModule, ModuleDependencies


class ABIModule(BaseModule):
    dependencies = ModuleDependencies(services=("vector_store", "model_registry"))
```

`model_registry` gives the module the engine's embedding model; it needs `naas-abi-sdk[models]`.

### Ensure the collection

Call it when the module starts, in `on_initialized`:

```python title="src/my_module/__init__.py"
    async def on_initialized(self):
        registry = self.engine.services.model_registry
        embedding = (await registry.get_default_embedding_model()).model
        dimension = len(await embedding.aembed_query("dimension probe"))
        await self.engine.services.vector_store.ensure_collection(
            "my_module_notes",
            dimension=dimension,
            distance_metric="cosine",
        )
```

`ensure_collection` creates the collection when it is missing. When it exists, it is left as is, even if its dimension differs. `recreate=True` drops it and creates it again, with all its vectors gone. Backend-specific options have no call: passing any other keyword raises `NotImplementedError`.

Take the dimension from the embedding model rather than a fixed number: the example embeds a short text to measure it, which calls the provider once. Collection names are global, so prefix them with the module.

### Check it

```python
store = self.engine.services.vector_store
await store.list_collections()                      # ["my_module_notes", ...]
await store.get_collection_info("my_module_notes")  # CollectionInfo(name, dimension, distance_metric, size)
```

:::warning Distance metrics
`sqlite_vec` accepts `cosine`, `euclidean`, `l2`, `dot` and `l1`. `qdrant` and `qdrant_in_memory` map `cosine`, `euclidean` and `dot`; any other name silently becomes `cosine`.
:::

## REST API

The System routes of the Nexus API. Auth: a Nexus session of a super admin (`POST /api/auth/login` returns the access token). Every change is audited.

### Write the first vector

Writing a vector to a collection that does not exist creates it. The collection takes the vector's length as its dimension, and `distance_metric` from the body (`cosine` when left out):

```bash
curl -X PUT "http://localhost:9879/api/admin/system/resources/vector_store/entry?id=my_module_notes/0b6f7c1e-6a4e-4a43-9a3c-6f0d6c1d2e11" \
  -H "Authorization: Bearer $NEXUS_TOKEN" \
  --data-binary '{"vector": [0.12, -0.03, 0.88], "metadata": {"source": "manual"}, "payload": {"text": "Hello"}, "distance_metric": "cosine"}'
```

The `id` is `<collection>/<document id>`. A real vector has the length of your embedding model's output, not three numbers. With `abi dev up`, use the `api` port that `abi dev ports` prints.

### List the collections

```http
GET /api/admin/system/resources/vector_store/entries
```

Each collection shows its size, dimension and distance metric.

## Config

Not applicable. `services.vector_store` in `config.yaml` picks the backend that holds the collections, not the collections. See [Vector store](/capabilities/get-started/configuration#vector-store).
