Chroma vector store
The ChromaVector class provides vector search using Chroma, an open-source embedding database. It offers efficient vector search with metadata filtering and hybrid search capabilities.
Chroma Cloud powers serverless vector and full-text search. It's extremely fast, cost-effective, scalable and painless. Create a DB and try it out in under 30 seconds with $5 of free credits.
Constructor optionsDirect link to Constructor options
host?:
port?:
ssl?:
apiKey?:
tenant?:
database?:
headers?:
fetchOptions?:
Running a Chroma serverDirect link to Running a Chroma server
If you are a Chroma Cloud user, provide the ChromaVector constructor your API key, tenant, and database name.
When you install the @mastra/chroma package, you get access to the Chroma CLI, which can set these as environment variables for you: chroma db connect [DB-NAME] --env-file.
Otherwise, you have several options for setting up your single-node Chroma server:
- Run one locally using the Chroma CLI:
chroma run. You can find more configuration options on the Chroma docs. - Run on Docker using the official Chroma image.
- Deploy your own Chroma server on your provider of choice. Chroma offers example templates for AWS, Azure, and GCP.
MethodsDirect link to Methods
createIndex()Direct link to createindex
indexName:
dimension:
metric?:
forkIndex()Direct link to forkindex
Note: Forking is only supported on Chroma Cloud, or if you deploy your own OSS distributed Chroma.
forkIndex lets you fork an existing Chroma index instantly. Operations on the forked index don't affect the original one. Learn more on the Chroma docs.
indexName:
newIndexName:
upsert()Direct link to upsert
indexName:
vectors:
metadata?:
ids?:
documents?:
query()Direct link to query
Query an index using a queryVector. Returns an array of semantically similar records in order of distance from the queryVector. Each record has the shape:
{
id: string;
score: number;
document?: string;
metadata?: Record<string, string | number | boolean>;
embedding?: number[]
}
You can also provide the shape of your metadata to a query call for type inference: query<T>().
indexName:
queryVector:
topK?:
filter?:
includeVector?:
documentFilter?:
get()Direct link to get
Get records from your Chroma index by IDs, metadata, and document filters. It returns an array of records of the shape:
{
id: string;
document?: string;
metadata?: Record<string, string | number | boolean>;
embedding?: number[]
}
You can also provide the shape of your metadata to a get call for type inference: get<T>().
indexName:
ids?:
filter?:
includeVector?:
documentFilter?:
limit?:
offset?:
listIndexes()Direct link to listindexes
Returns an array of index names as strings.
describeIndex()Direct link to describeindex
indexName:
Returns:
interface IndexStats {
dimension: number
count: number
metric: 'cosine' | 'euclidean' | 'dotproduct'
}
deleteIndex()Direct link to deleteindex
indexName:
updateVector()Direct link to updatevector
Update a single vector by ID or by metadata filter. Either id or filter must be provided, but not both.
indexName:
id?:
filter?:
update:
The update object can contain:
vector?:
metadata?:
Example:
// Update by ID
await vectorStore.updateVector({
indexName: 'docs',
id: 'vec_123',
update: { metadata: { status: 'reviewed' } },
})
// Update by filter
await vectorStore.updateVector({
indexName: 'docs',
filter: { source_id: 'manual.pdf' },
update: { metadata: { version: 2 } },
})
deleteVector()Direct link to deletevector
indexName:
id:
deleteVectors()Direct link to deletevectors
Delete multiple vectors by IDs or by metadata filter. This method enables bulk deletion and source-based vector management. Either ids or filter must be provided, but not both.
indexName:
ids?:
filter?:
Example:
// Delete all chunks from a document
await vectorStore.deleteVectors({
indexName: 'docs',
filter: { source_id: 'manual.pdf' },
})
// Delete multiple vectors by ID
await vectorStore.deleteVectors({
indexName: 'docs',
ids: ['vec_1', 'vec_2', 'vec_3'],
})
// Delete old temporary documents
await vectorStore.deleteVectors({
indexName: 'docs',
filter: {
$and: [{ bucket: 'temp' }, { indexed_at: { $lt: '2025-01-01' } }],
},
})
Response typesDirect link to Response types
Query results are returned in this format:
interface QueryResult {
id: string
score: number
metadata: Record<string, any>
document?: string // Chroma-specific: Original document if it was stored
vector?: number[] // Only included if includeVector is true
}
Error handlingDirect link to Error handling
The store throws typed errors that can be caught:
try {
await store.query({
indexName: 'index_name',
queryVector: queryVector,
})
} catch (error) {
if (error instanceof VectorStoreError) {
console.log(error.code) // 'connection_failed' | 'invalid_dimension' | etc
console.log(error.details) // Additional error context
}
}