Skip to content

A Simple and Universal Swarm Intelligence Engine, Predicting Anything. 简洁通用的群体智能引擎,预测万物

fix: handle string attributes in graph ontology to prevent TypeError crash (fixes #135)

octo-patcha07841e91bbb
diff --git a/backend/app/services/graph_builder.py b/backend/app/services/graph_builder.py
index 37c9969c..a882377f 100644
--- a/backend/app/services/graph_builder.py
+++ b/backend/app/services/graph_builder.py
@@ -233,8 +233,13 @@ class GraphBuilderService:
annotations = {}
for attr_def in entity_def.get("attributes", []):
- attr_name = safe_attr_name(attr_def["name"]) # 使用安全名称
- attr_desc = attr_def.get("description", attr_name)
+ # LLM may return attributes as strings instead of dicts
+ if isinstance(attr_def, str):
+ attr_name = safe_attr_name(attr_def)
+ attr_desc = attr_def
+ else:
+ attr_name = safe_attr_name(attr_def["name"]) # 使用安全名称
+ attr_desc = attr_def.get("description", attr_name)
# Zep API 需要 Field 的 description,这是必需的
attrs[attr_name] = Field(description=attr_desc, default=None)
annotations[attr_name] = Optional[EntityText] # 类型注解
@@ -257,8 +262,13 @@ class GraphBuilderService:
annotations = {}
for attr_def in edge_def.get("attributes", []):
- attr_name = safe_attr_name(attr_def["name"]) # 使用安全名称
- attr_desc = attr_def.get("description", attr_name)
+ # LLM may return attributes as strings instead of dicts
+ if isinstance(attr_def, str):
+ attr_name = safe_attr_name(attr_def)
+ attr_desc = attr_def
+ else:
+ attr_name = safe_attr_name(attr_def["name"]) # 使用安全名称
+ attr_desc = attr_def.get("description", attr_name)
# Zep API 需要 Field 的 description,这是必需的
attrs[attr_name] = Field(description=attr_desc, default=None)
annotations[attr_name] = Optional[str] # 边属性用str类型
diff --git a/backend/app/services/ontology_generator.py b/backend/app/services/ontology_generator.py
index 7619a79f..0d8432c4 100644
--- a/backend/app/services/ontology_generator.py
+++ b/backend/app/services/ontology_generator.py
@@ -432,6 +432,11 @@ class OntologyGenerator:
entity_name_map[original_name] = entity["name"]
if "attributes" not in entity:
entity["attributes"] = []
+ # Normalize attributes: LLM may return strings instead of dicts
+ entity["attributes"] = [
+ attr if isinstance(attr, dict) else {"name": attr, "type": "text", "description": attr}
+ for attr in entity["attributes"]
+ ]
if "examples" not in entity:
entity["examples"] = []
# 确保description不超过100字符
@@ -456,6 +461,11 @@ class OntologyGenerator:
edge["source_targets"] = []
if "attributes" not in edge:
edge["attributes"] = []
+ # Normalize attributes: LLM may return strings instead of dicts
+ edge["attributes"] = [
+ attr if isinstance(attr, dict) else {"name": attr, "type": "text", "description": attr}
+ for attr in edge["attributes"]
+ ]
if len(edge.get("description", "")) > 100:
edge["description"] = edge["description"][:97] + "..."