# `TypeDB.Vector`
[🔗](https://github.com/NoeticEcho/TypedbEx/blob/v0.11.1/typedb/lib/typedb/vector.ex#L1)

Vector values — **experimental**. They need a TypeDB build with vector
support, which no release is yet, started with
`--experimental-vector-search-enabled=true`; the syntax and the wire format
may change until TypeDB releases them.

A vector attribute is declared with its dimension and dtype (`float32` is the
only one), and searched by cosine similarity through an HNSW index the
attribute declares:

    define
      attribute embedding, value vector(1024, "float32") @index(hnsw:cosine);
      entity memory, owns embedding;

    given $q: vector(1024, "float32");
    match let $e, $score in cosine_similarity_search(embedding, $q, 0.55);
      $m isa memory, has embedding $e;
    select $m, $score; sort $score desc; limit 10;

`$score` is a `double`, and the search returns every match above the
threshold, so `sort` and `limit` are what make it a top-k. A query with
several `given` rows runs once per row and sorts and limits across all of
them.

`value/1` builds the term to pass in `given_rows`, and the same term works
over `typedb_grpc`:

    TypeDB.query(conn, "memories", query,
      transaction_type: :read,
      given_rows: [%{"q" => TypeDB.Vector.value!(embedding)}]
    )

## What the driver checks, and what the server does

Before a request is sent, a vector's length must match the dimension its
`value_type` declares, and every component must be a number a `float32`
holds. Anything else is `TypeDB.Error` with kind `:encode`, raised from
`TypeDB.Given` as for any value it cannot encode, so nothing reaches the
server. A number past `float32`'s range would otherwise become an infinity
on the server.

Observed on the build `3.14.1-dev`, commit `11ab292c` (2026-11-12): the
server refuses a stored vector with a non-finite component (`DVL23`) and a
query vector of zero norm (`COR32`); a stored zero vector matches nothing;
`sort` on a vector-valued variable is refused (`QUA20`). **A similarity
search inside `not { }` or inside a branch of `or { }` panics the server
process**, taking every database down; keep the search in the query's top
conjunction. A negation over a relation beside a search is fine.

# `value`

```elixir
@spec value([number()]) ::
  {:ok, TypeDB.Concept.Value.t()} | {:error, TypeDB.Error.t()}
```

The `given` term for a vector of these components, as `vector(N, "float32")`.

    iex> TypeDB.Vector.value([1, 0.5])
    {:ok, %TypeDB.Concept.Value{value: [1.0, 0.5], value_type: ~s|vector(2, "float32")|}}

    iex> TypeDB.Vector.value([1.0e39])
    {:error, %TypeDB.Error{kind: :encode, message: "cannot encode [1.0e39] as a vector: expected a non-empty list of numbers, each a finite float32"}}

# `value!`

```elixir
@spec value!([number()]) :: TypeDB.Concept.Value.t()
```

As `value/1`, raising the error.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
