From 5cdaf3c49428e0da70d6e93a426649988325982f Mon Sep 17 00:00:00 2001 From: Henry Heng Date: Fri, 5 Dec 2025 12:55:45 +0000 Subject: [PATCH] Bugfix/Chroma Metadata (#5552) * update Chroma metadata handling with sanitization function * Update packages/components/nodes/vectorstores/Chroma/core.ts Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- .../nodes/vectorstores/Chroma/core.ts | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/components/nodes/vectorstores/Chroma/core.ts b/packages/components/nodes/vectorstores/Chroma/core.ts index 7d1bfa1e..eeb65161 100644 --- a/packages/components/nodes/vectorstores/Chroma/core.ts +++ b/packages/components/nodes/vectorstores/Chroma/core.ts @@ -5,6 +5,7 @@ import type { Collection, CollectionConfiguration, CollectionMetadata, + Metadata, Where } from 'chromadb' @@ -145,7 +146,7 @@ export class Chroma extends VectorStore { const documentIds = options?.ids ?? Array.from({ length: vectors.length }, () => uuid.v1()) const collection = await this.ensureCollection() - const mappedMetadatas = documents.map(({ metadata }) => { + const mappedMetadatas: Metadata[] = documents.map(({ metadata }) => { let locFrom let locTo @@ -162,7 +163,7 @@ export class Chroma extends VectorStore { if (newMetadata.loc) delete newMetadata.loc - return newMetadata + return sanitizeMetadata(newMetadata) }) await collection.upsert({ @@ -343,3 +344,27 @@ function ensureCollectionName(collectionName?: string) { } return collectionName } + +/** + * Sanitizes metadata to only include Chroma-compatible primitive values. + * Chroma metadata only supports boolean, number, string, and null values. + * Arrays and objects are JSON stringified to preserve the data. + */ +function sanitizeMetadata(metadata: Document['metadata']): Metadata { + const sanitized: Metadata = {} + for (const [key, value] of Object.entries(metadata)) { + if (value === null || typeof value === 'boolean' || typeof value === 'number' || typeof value === 'string') { + sanitized[key] = value + } else if (value !== undefined) { + try { + const stringified = JSON.stringify(value) + if (stringified !== undefined) { + sanitized[key] = stringified + } + } catch { + // Skip values that cannot be stringified (e.g. circular references) + } + } + } + return sanitized +}