Skip to content

Commit 9b2e4d2

Browse files
committed
fix: enable ChromaDB as vector memory backend for agent chat
Fixes integration issue preventing ChromaDB from being used as VectorMemory backend. The root cause is that VectorMemory stores sub_dicts as a list in node metadata, which is rejected by vector stores that require flat metadata values (ChromaDB, SingleStoreDB, etc.). Changes: - In _commit_node(): serialize sub_dicts to JSON string before insert_nodes, restore list form after for in-memory operations - In get(): handle both serialized JSON string and raw list forms of sub_dicts for backward compatibility Fixes #15681
1 parent 9aa5ee5 commit 9b2e4d2

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

llama-index-core/llama_index/core/memory/vector_memory.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
"""
77

8+
import json
89
import uuid
910
from typing import Any, Dict, List, Optional, Union
1011

@@ -144,11 +145,20 @@ def get(
144145
nodes = retriever.retrieve(input or "")
145146

146147
# retrieve underlying messages
147-
return [
148-
ChatMessage.model_validate(sub_dict)
149-
for node in nodes
150-
for sub_dict in node.metadata["sub_dicts"]
151-
]
148+
messages: List[ChatMessage] = []
149+
for node in nodes:
150+
sub_dicts = node.metadata.get("sub_dicts", [])
151+
# Handle both serialized (JSON string, for flat-metadata vector
152+
# stores like ChromaDB) and raw list forms for backward
153+
# compatibility.
154+
if isinstance(sub_dicts, str):
155+
try:
156+
sub_dicts = json.loads(sub_dicts)
157+
except json.JSONDecodeError:
158+
continue
159+
for sub_dict in sub_dicts:
160+
messages.append(ChatMessage.model_validate(sub_dict))
161+
return messages
152162

153163
def get_all(self) -> List[ChatMessage]:
154164
"""Get all chat history."""
@@ -172,7 +182,18 @@ def _commit_node(self, override_last: bool = False) -> None:
172182
# logic in self.put().)
173183
self.vector_index.delete_nodes([self.cur_batch_textnode.id_])
174184

175-
self.vector_index.insert_nodes([self.cur_batch_textnode])
185+
# Serialize sub_dicts to a JSON string for compatibility with vector
186+
# stores that require flat metadata (e.g. ChromaDB, SingleStoreDB).
187+
# The original list form is restored after insertion so that in-memory
188+
# append operations in put() continue to work.
189+
original_sub_dicts = self.cur_batch_textnode.metadata["sub_dicts"]
190+
self.cur_batch_textnode.metadata["sub_dicts"] = json.dumps(
191+
original_sub_dicts
192+
)
193+
try:
194+
self.vector_index.insert_nodes([self.cur_batch_textnode])
195+
finally:
196+
self.cur_batch_textnode.metadata["sub_dicts"] = original_sub_dicts
176197

177198
def put(self, message: ChatMessage) -> None:
178199
"""Put chat history."""

0 commit comments

Comments
 (0)