fix: handle missing 'weight' attribute in edge data to prevent KeyError

- Add validation in _find_most_related_edges_from_entities and  _get_edge_data function during edge data construction
- Add warning logs when 'weight' attribute is missing and set default value of 0.0
This commit is contained in:
yangdx
2025-05-11 11:16:32 +08:00
parent 5a3bf5ecc8
commit 68653f853a

View File

@@ -1609,6 +1609,10 @@ async def _find_most_related_edges_from_entities(
for pair in all_edges:
edge_props = edge_data_dict.get(pair)
if edge_props is not None:
if "weight" not in edge_props:
logger.warning(f"Edge {pair} missing 'weight' attribute, using default value 0.0")
edge_props["weight"] = 0.0
combined = {
"src_tgt": pair,
"rank": edge_degrees_dict.get(pair, 0),
@@ -1670,6 +1674,10 @@ async def _get_edge_data(
pair = (k["src_id"], k["tgt_id"])
edge_props = edge_data_dict.get(pair)
if edge_props is not None:
if "weight" not in edge_props:
logger.warning(f"Edge {pair} missing 'weight' attribute, using default value 0.0")
edge_props["weight"] = 0.0
# Use edge degree from the batch as rank.
combined = {
"src_id": k["src_id"],