fix(backends/chroma): catch-all metadata sanitizer in add/upsert
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) <noreply@anthropic.com>
This commit is contained in:
@@ -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():
|
||||
|
||||
Reference in New Issue
Block a user