From 68653f853ae98018432d4a5b6196de6884bc2377 Mon Sep 17 00:00:00 2001 From: yangdx Date: Sun, 11 May 2025 11:16:32 +0800 Subject: [PATCH] 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 --- lightrag/operate.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lightrag/operate.py b/lightrag/operate.py index 0d5c7866..41cb8cd4 100644 --- a/lightrag/operate.py +++ b/lightrag/operate.py @@ -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"],