# VECTOR.ADD

> Add or replace a vector in an index.

Use `VECTOR.ADD` to store a vector under an ID in an index.

The write is an upsert: an ID that is not in the index is added and the reply is `1`, and an ID that is already there has its vector replaced and the reply is `0`. Replacing a vector does not change [`VECTOR.COUNT`](/redis/commands/vector/vector-count), so the reply is what tells a bulk loader how many documents it actually introduced.

The vector can be given three ways. `VALUES` takes a count followed by that many decimal numbers. `FP32` takes the raw little-endian 32-bit float blob, which skips decimal formatting and parsing on a binary-safe connection. `BASE64-FP32` takes that same blob base64-encoded, which is how a binary vector travels over the [REST API](/redis/features/restapi), where arguments are JSON strings. Whichever form is used, the length must match the index's `DIM`.

Vector indexes are an Upstash extension. See the [vector command overview](/redis/commands/vector/overview) for how an index is created, written to, and queried.

## Syntax

```redis
VECTOR.ADD <index> <id>
  <VALUES <count> <element> [<element> ...] | FP32 <blob> | BASE64-FP32 <blob>>
```

## Arguments

| Argument | Required | Repeatable | Description |
| --- | --- | --- | --- |
| `<index>` | Yes | No | Key holding the vector index. |
| `<id>` | Yes | No | Identifier to store the vector under. Cannot be empty. |
| `VALUES <count> <element> [<element> ...]` | No | No | Vector given as `<count>` decimal elements. |
| `FP32 <blob>` | No | No | Vector given as a raw binary blob of little-endian 32-bit floats. Its length must be a non-zero multiple of 4. |
| `BASE64-FP32 <blob>` | No | No | Vector given as the standard base64 encoding of an `FP32` blob. |

## Important points

- The three vector forms are mutually exclusive and exactly one must be given. `VALUES` is the readable form; `FP32` avoids decimal formatting on a binary-safe connection; `BASE64-FP32` carries the same bytes through JSON, which is what the [REST API](/redis/features/restapi) needs.
- A vector whose length differs from the index's `DIM` returns `ERR vector dimension mismatch: expected <dim>, got <n>`.
- An index that does not exist returns `ERR Index <index> is not found`. Create it with [`VECTOR.CREATE`](/redis/commands/vector/vector-create) first.

## Response

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

| Protocol | Reply |
| --- | --- |
| RESP2 | Integer: `1` if the ID was added, `0` if an existing vector was replaced |
| RESP3 | Integer: `1` if the ID was added, `0` if an existing vector was replaced |

<Note>
  Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
</Note>

## Examples

TCP examples use the TLS `REDIS_URL` from the Upstash console. REST examples use `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN`.

<AccordionGroup>

<Accordion title="Redis CLI" icon="terminal">

```bash
VECTOR.ADD my-index doc:1 VALUES 3 0.1 0.2 0.3
```

</Accordion>

<Accordion title="@upstash/redis" icon="node-js" iconType="brands">

<Note>
  This command is not supported yet in `@upstash/redis`.
</Note>

</Accordion>

<Accordion title="upstash_redis" icon="python" iconType="brands">

<Note>
  This command is not supported yet in `upstash_redis`.
</Note>

</Accordion>

<Accordion title="ioredis" icon="node-js" iconType="brands">

```ts
import Redis from "ioredis";

const redis = new Redis(process.env.REDIS_URL!);
const result = await redis.call("VECTOR.ADD", "my-index", "doc:1", "VALUES", "3", "0.1", "0.2", "0.3");
console.log(result);
```

</Accordion>

<Accordion title="node-redis" icon="node-js" iconType="brands">

```ts
import { createClient } from "redis";

const client = await createClient({ url: process.env.REDIS_URL })
  .on("error", console.error)
  .connect();
const result = await client.sendCommand(["VECTOR.ADD", "my-index", "doc:1", "VALUES", "3", "0.1", "0.2", "0.3"]);
console.log(result);
```

</Accordion>

<Accordion title="redis-py" icon="python" iconType="brands">

```python
import os
import redis

client = redis.from_url(os.environ["REDIS_URL"])
result = client.execute_command("VECTOR.ADD", "my-index", "doc:1", "VALUES", "3", "0.1", "0.2", "0.3")
print(result)
```

</Accordion>

<Accordion title="go-redis" icon="golang" iconType="brands">

```go
package main

import (
    "context"
    "fmt"
    "os"

    "github.com/redis/go-redis/v9"
)

func main() {
    opts, err := redis.ParseURL(os.Getenv("REDIS_URL"))
    if err != nil {
        panic(err)
    }
    client := redis.NewClient(opts)
    result, err := client.Do(context.Background(), "VECTOR.ADD", "my-index", "doc:1", "VALUES", "3", "0.1", "0.2", "0.3").Result()
    if err != nil {
        panic(err)
    }
    fmt.Println(result)
}
```

</Accordion>

<Accordion title="jedis" icon="java" iconType="brands">

```java
import java.net.URI;

import redis.clients.jedis.Jedis;

try (Jedis jedis = new Jedis(new URI(System.getenv("REDIS_URL")))) {
  Object result = jedis.sendCommand(() -> "VECTOR.ADD".getBytes(), "my-index", "doc:1", "VALUES", "3", "0.1", "0.2", "0.3");
  System.out.println(result);
}
```

</Accordion>

<Accordion title="redis-rs" icon="rust" iconType="brands">

```rust
fn main() -> redis::RedisResult<()> {
    let url = std::env::var("REDIS_URL").expect("REDIS_URL is not set");
    let client = redis::Client::open(url)?;
    let mut connection = client.get_connection()?;

    let mut command = redis::cmd("VECTOR.ADD");
    command.arg("my-index");
    command.arg("doc:1");
    command.arg("VALUES");
    command.arg("3");
    command.arg("0.1");
    command.arg("0.2");
    command.arg("0.3");
    let result: redis::Value = command.query(&mut connection)?;
    println!("{result:?}");
    Ok(())
}
```

</Accordion>

</AccordionGroup>
