Vectorize API
This page covers the Vectorize API available within Cloudflare Workers, including usage examples.
Operations
Insert vectors
Inserts vectors into the index. Vectorize inserts are asynchronous and the insert operation returns a mutation identifier unique for that operation. It typically takes a few seconds for inserted vectors to be available for querying in a Vectorize index.
If vectors with the same vector ID already exist in the index, only the vectors with new IDs will be inserted.
If you need to update existing vectors, use the upsert operation.
Upsert vectors
Upserts vectors into an index. Vectorize upserts are asynchronous and the upsert operation returns a mutation identifier unique for that operation. It typically takes a few seconds for upserted vectors to be available for querying in a Vectorize index.
An upsert operation will insert vectors into the index if vectors with the same ID do not exist, and overwrite vectors with the same ID.
Upserting does not merge or combine the values or metadata of an existing vector with the upserted vector: the upserted vector replaces the existing vector in full.
Query vectors
Query an index with the provided vector, returning the score(s) of the closest vectors based on the configured distance metric.
- Configure the number of returned matches by setting
topK
(default: 5) - Return vector values by setting
returnValues: true
(default: false) - Return vector metadata by setting
returnMetadata: 'indexed'
orreturnMetadata: 'all'
(default: ‘none’)
topK
The topK
can be configured to specify the number of matches returned by the query operation. Vectorize now supports an upper limit of 100
for the topK
value. However, for a query operation with returnValues
set to true
or returnMetadata
set to all
, topK
would be limited to a maximum value of 20
.
returnMetadata
The returnMetadata
field provides three ways to fetch vector metadata while querying:
none
: Do not fetch metadata.indexed
: Fetched metadata only for the indexed metadata fields. There is no latency overhead with this option, but long text fields may be truncated.all
: Fetch all metadata associated with a vector. Queries may run slower with this option, andtopK
would be limited to 20.
Query vectors by ID
Query an index using a vector that is already present in the index.
Query options remain the same as the query operation described above.
Get vectors by ID
Retrieves the specified vectors by their ID, including values and metadata.
Delete vectors by ID
Deletes the vector IDs provided from the current index. Vectorize deletes are asynchronous and the delete operation returns a mutation identifier unique for that operation. It typically takes a few seconds for vectors to be removed from the Vectorize index.
Retrieve index details
Retrieves the configuration of a given index directly, including its configured dimensions
and distance metric
.
Create Metadata Index
Enable metadata filtering on the specified property. Limited to 10 properties.
Run the following wrangler vectorize
command:
Delete Metadata Index
Allow Vectorize to delete the specified metadata index.
Run the following wrangler vectorize
command:
List Metadata Indexes
List metadata properties on which metadata filtering is enabled.
Run the following wrangler vectorize
command:
Get Index Info
Get additional details about the index.
Run the following wrangler vectorize
command:
Vectors
A vector represents the vector embedding output from a machine learning model.
id
- a uniquestring
identifying the vector in the index. This should map back to the ID of the document, object or database identifier that the vector values were generated from.namespace
- an optional partition key within a index. Operations are performed per-namespace, so this can be used to create isolated segments within a larger index.values
- an array ofnumber
,Float32Array
, orFloat64Array
as the vector embedding itself. This must be a dense array, and the length of this array must match thedimensions
configured on the index.metadata
- an optional set of key-value pairs that can be used to store additional metadata alongside a vector.
Binding to a Worker
Bindings allow you to attach resources, including Vectorize indexes or R2 buckets, to your Worker.
Bindings are defined in either the wrangler.toml
configuration associated with your Workers project, or via the Cloudflare dashboard for your project.
Vectorize indexes are bound by name. A binding for an index named production-doc-search
would resemble the below:
Refer to the bindings documentation for more details.
TypeScript Types
New Workers projects created via npm create cloudflare@latest
automatically include the relevant TypeScript types for Vectorize.
Older projects, or non-Workers projects looking to use Vectorize’s REST API ↗ in a TypeScript project, should ensure @cloudflare/workers-types
version 4.20230922.0
or later is installed.