From 5d95656b6be98dd6dbdc1064e434e67dd2d82cb9 Mon Sep 17 00:00:00 2001 From: jp Date: Mon, 11 May 2026 12:10:48 -0700 Subject: [PATCH] fix(backends/chroma): catch-all metadata sanitizer in add/upsert MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Belt-and-suspenders on top of the repair.py sanitization in the previous commit. A 151,478-drawer rebuild test still failed at ~120K with the same `ValueError: Expected metadata to be a non-empty dict` from chromadb, even with the repair.py sanitizers in place. Traceback: mempalace/backends/chroma.py:add → chromadb Collection.add → validate_insert_record_set → validate_metadatas → validate_metadata → ValueError Likely cause: chromadb's `upsert()` internally calls `add()` for new records, and somewhere between repair.py's batch upsert and chromadb's final write, the metadatas list gets reprocessed in a way that re-introduces empty/None entries. Sanitizing at the chromadb-client chokepoint catches everything: no caller can leak bad metadata regardless of upstream sanitization state. Same `{"_repaired_empty_meta": True}` sentinel, searchable via `where={"_repaired_empty_meta": True}`. Cost: one list comprehension per write call; negligible vs the embedding + HNSW work each upsert already does. Co-Authored-By: Claude Opus 4.7 (1M context) --- mempalace/backends/chroma.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/mempalace/backends/chroma.py b/mempalace/backends/chroma.py index fe36f34..499d000 100644 --- a/mempalace/backends/chroma.py +++ b/mempalace/backends/chroma.py @@ -873,10 +873,30 @@ class ChromaCollection(BaseCollection): # Writes # ------------------------------------------------------------------ + @staticmethod + def _sanitize_metadatas_for_chromadb(metadatas): + """chromadb 1.5.x rejects None and empty-dict entries in the metadatas + list (ValueError: Expected metadata to be a non-empty dict, got 0 + metadata attributes in add). Coerce any such entry to a sentinel so + the write succeeds. Operators can later locate coerced drawers via + ``where={"_repaired_empty_meta": True}``. + + This is the chokepoint catch-all: even if a caller's own sanitizer + misses a case (or skips for performance), reaching the chromadb + client always goes through here first. + """ + if metadatas is None: + return None + return [ + m if (isinstance(m, dict) and len(m) > 0) else {"_repaired_empty_meta": True} + for m in metadatas + ] + def add(self, *, documents, ids, metadatas=None, embeddings=None): kwargs: dict[str, Any] = {"documents": documents, "ids": ids} - if metadatas is not None: - kwargs["metadatas"] = metadatas + sanitized = self._sanitize_metadatas_for_chromadb(metadatas) + if sanitized is not None: + kwargs["metadatas"] = sanitized if embeddings is not None: kwargs["embeddings"] = embeddings with self._write_lock(): @@ -884,8 +904,9 @@ class ChromaCollection(BaseCollection): def upsert(self, *, documents, ids, metadatas=None, embeddings=None): kwargs: dict[str, Any] = {"documents": documents, "ids": ids} - if metadatas is not None: - kwargs["metadatas"] = metadatas + sanitized = self._sanitize_metadatas_for_chromadb(metadatas) + if sanitized is not None: + kwargs["metadatas"] = sanitized if embeddings is not None: kwargs["embeddings"] = embeddings with self._write_lock():