fix: harden normalize_facts with None guard and skip unknown dict shapes
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
|
import logging
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from mem0.configs.prompts import (
|
from mem0.configs.prompts import (
|
||||||
@@ -7,6 +8,8 @@ from mem0.configs.prompts import (
|
|||||||
USER_MEMORY_EXTRACTION_PROMPT,
|
USER_MEMORY_EXTRACTION_PROMPT,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def get_fact_retrieval_messages(message, is_agent_memory=False):
|
def get_fact_retrieval_messages(message, is_agent_memory=False):
|
||||||
"""Get fact retrieval messages based on the memory type.
|
"""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.
|
like {"fact": "..."} or {"text": "..."} instead of plain strings.
|
||||||
This mirrors the TypeScript FactRetrievalSchema validation.
|
This mirrors the TypeScript FactRetrievalSchema validation.
|
||||||
"""
|
"""
|
||||||
|
if not raw_facts:
|
||||||
|
return []
|
||||||
normalized = []
|
normalized = []
|
||||||
for item in raw_facts:
|
for item in raw_facts:
|
||||||
if isinstance(item, str):
|
if isinstance(item, str):
|
||||||
fact = item
|
fact = item
|
||||||
elif isinstance(item, dict):
|
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:
|
else:
|
||||||
fact = str(item)
|
fact = str(item)
|
||||||
if fact:
|
if fact:
|
||||||
|
|||||||
Reference in New Issue
Block a user