fix(mem0): handle dict facts from LLM and fix slice error on dicts
The extraction LLM returns facts as {"fact": "...", "date": "..."} dicts
instead of plain strings. store_facts now normalizes these to strings
before passing to mem0.add(). Also fixes KeyError when slicing dicts
in the error handler.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -212,16 +212,23 @@ class Mem0MemoryStore:
|
||||
|
||||
stored = 0
|
||||
for fact in facts:
|
||||
# Normalize: LLM may return dicts like {"fact": "...", "date": "..."} or plain strings
|
||||
if isinstance(fact, dict):
|
||||
fact_text = fact.get("fact", fact.get("text", str(fact)))
|
||||
else:
|
||||
fact_text = str(fact)
|
||||
if not fact_text.strip():
|
||||
continue
|
||||
try:
|
||||
self.memory.add(
|
||||
fact,
|
||||
fact_text,
|
||||
user_id=user_id,
|
||||
infer=False,
|
||||
metadata=metadata if metadata else None,
|
||||
)
|
||||
stored += 1
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to store fact '{fact[:50]}...': {e}")
|
||||
logger.error(f"Failed to store fact '{str(fact_text)[:50]}...': {e}")
|
||||
|
||||
logger.info(f"Stored {stored}/{len(facts)} facts for user {user_id}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user