fix: harden normalize_facts with None guard and skip unknown dict shapes

This commit is contained in:
kartik-mem0
2026-03-18 20:09:24 +05:30
parent 99797a183b
commit 2fc9368f9b
+9 -1
View File
@@ -1,4 +1,5 @@
import hashlib
import logging
import re
from mem0.configs.prompts import (
@@ -7,6 +8,8 @@ from mem0.configs.prompts import (
USER_MEMORY_EXTRACTION_PROMPT,
)
logger = logging.getLogger(__name__)
def get_fact_retrieval_messages(message, is_agent_memory=False):
"""Get fact retrieval messages based on the memory type.
@@ -59,12 +62,17 @@ def normalize_facts(raw_facts):
like {"fact": "..."} or {"text": "..."} instead of plain strings.
This mirrors the TypeScript FactRetrievalSchema validation.
"""
if not raw_facts:
return []
normalized = []
for item in raw_facts:
if isinstance(item, str):
fact = item
elif isinstance(item, dict):
fact = item.get("fact") or item.get("text") or str(item)
fact = item.get("fact") or item.get("text")
if fact is None:
logger.warning("Unexpected fact shape from LLM, skipping: %s", item)
continue
else:
fact = str(item)
if fact: