Skip to content

Commit 58b4e1d

Browse files
committed
feat: fix polardb
1 parent 7f69ab6 commit 58b4e1d

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/memos/graph_dbs/polardb.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,7 +2193,24 @@ def _strip_wrapping_quotes(value: Any) -> Any:
21932193
if time_field in node and hasattr(node[time_field], "isoformat"):
21942194
node[time_field] = node[time_field].isoformat()
21952195

2196-
# Do not deserialize sources and usage; keep List[str]
2196+
# Deserialize sources, usage and tags if they are not lists
2197+
for field_name in ["sources", "usage", "tags"]:
2198+
if field_name in node and node[field_name] is not None:
2199+
field_value = node[field_name]
2200+
2201+
# If it's a string, try to parse it as JSON
2202+
if isinstance(field_value, str):
2203+
try:
2204+
node[field_name] = json.loads(field_value)
2205+
except (json.JSONDecodeError, TypeError):
2206+
logger.warning(f"Failed to parse {field_name} as JSON, wrapping in list")
2207+
node[field_name] = [field_value]
2208+
2209+
# If it's not a list, wrap it in a list
2210+
elif not isinstance(field_value, list):
2211+
logger.warning(f"{field_name} is not a list, wrapping value: {type(field_value)}")
2212+
node[field_name] = [field_value]
2213+
21972214
# Do not remove user_name; keep all fields
21982215

21992216
return {"id": node.pop("id"), "memory": node.pop("memory", ""), "metadata": node}
@@ -2386,7 +2403,25 @@ def _build_node_from_agtype(self, node_agtype, embedding=None):
23862403
if embedding is not None:
23872404
props["embedding"] = embedding
23882405

2389-
# Return standard format directly; no need to call _parse_node_new again
2406+
# Deserialize sources, usage and tags if they are not lists
2407+
for field_name in ["sources", "usage", "tags"]:
2408+
if field_name in props and props[field_name] is not None:
2409+
field_value = props[field_name]
2410+
2411+
# If it's a string, try to parse it as JSON
2412+
if isinstance(field_value, str):
2413+
try:
2414+
props[field_name] = json.loads(field_value)
2415+
except (json.JSONDecodeError, TypeError):
2416+
logger.warning(f"Failed to parse {field_name} as JSON, wrapping in list")
2417+
props[field_name] = [field_value]
2418+
2419+
# If it's not a list, wrap it in a list
2420+
elif not isinstance(field_value, list):
2421+
logger.warning(f"{field_name} is not a list, wrapping value: {type(field_value)}")
2422+
props[field_name] = [field_value]
2423+
2424+
# Return standard format directly
23902425
return {"id": props.get("id", ""), "memory": props.get("memory", ""), "metadata": props}
23912426
except Exception:
23922427
return None

0 commit comments

Comments
 (0)