Skip to main content

Upsert data

Update a single existing entry matched by a predicate — replace its vector, its metadata, or both.

Every operation works on both Vector DB stores (you supply raw vectors) and AI stores (you supply text or images and Ahnlich AI generates the embeddings for you). Use the Vector DB / AI switch in the sample below.

Parameters

ParameterTypeDescription
storestringName of the target store.
conditionpredicateA predicate that must match exactly one entry.
new_keyvector · optionalNew vector to replace the matched entry's key.
new_valuemetadata · optionalMetadata to update on the matched entry.
merge_metadatabool · optionaltrue merges into existing metadata; false (default) replaces it entirely.
schemastring · optionalSchema to target. Defaults to public.

All requests accept an optional schema field. When omitted, the server uses the public schema.

Behavior

  • Errors if zero or multiple entries match the predicate — it targets a single entry.
  • Update the vector only, the metadata only, or both in one call.
  • With merge_metadata: true, unchanged fields are preserved.

Sample query

Python
import asyncio
from grpclib.client import Channel
from ahnlich_client_py.grpc import keyval, metadata, predicates
from ahnlich_client_py.grpc.services.db_service import DbServiceStub
from ahnlich_client_py.grpc.db import query as db_query


async def upsert():
async with Channel(host="127.0.0.1", port=1369) as channel:
client = DbServiceStub(channel)

condition = predicates.PredicateCondition(
value=predicates.Predicate(
equals=predicates.Equals(
key="id",
value=metadata.MetadataValue(raw_string="123")
)
)
)

new_value = keyval.StoreValue(
value={"status": metadata.MetadataValue(raw_string="published")}
)

response = await client.upsert(
db_query.Upsert(
store="my_store",
schema="analytics",
condition=condition,
new_value=new_value,
merge_metadata=True
)
)

if __name__ == "__main__":
asyncio.run(upsert())

Response

A Set response with upsert counts (e.g. inserted: 0, updated: 1).