Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Fix Graph.filter() crash on ids that don't exist in the graph

chuenchen3097791174c1b0b
diff --git a/src/python/txtai/graph/base.py b/src/python/txtai/graph/base.py
index 8262e74d..1f489b36 100644
--- a/src/python/txtai/graph/base.py
+++ b/src/python/txtai/graph/base.py
@@ -544,8 +544,13 @@ class Graph:
# Unpack node and score, if available
node, score = node if isinstance(node, tuple) else (node, None)
+ # Skip ids that don't exist in this graph
+ nodeattrs = self.node(node)
+ if nodeattrs is None:
+ continue
+
# Add nodes
- graph.addnode(node, **self.node(node))
+ graph.addnode(node, **nodeattrs)
# Add score if present
if score is not None:
diff --git a/test/python/testgraph.py b/test/python/testgraph.py
index 59c0f1fa..4443aef4 100644
--- a/test/python/testgraph.py
+++ b/test/python/testgraph.py
@@ -188,6 +188,26 @@ class TestGraph(unittest.TestCase):
self.assertTrue(graph.hasedge(0))
self.assertTrue(graph.hasedge(0, 1))
+ def testFilterUnknownId(self):
+ """
+ Test that filter() skips ids that don't exist in the graph instead of
+ crashing when unpacking self.node(node) (which returns None for a
+ missing node).
+ """
+
+ graph = GraphFactory.create({})
+ graph.initialize()
+ graph.addnode(0, id="a", data="hello")
+ graph.addnode(1, id="b", data="world")
+ graph.addedge(0, 1)
+
+ subgraph = graph.filter([0, 1, 99])
+
+ self.assertTrue(subgraph.hasnode(0))
+ self.assertTrue(subgraph.hasnode(1))
+ self.assertFalse(subgraph.hasnode(99))
+ self.assertTrue(subgraph.hasedge(0, 1))
+
def testInsertNoneTextIndexSync(self):
"""
Test that a document with a present but None text/object field consumes an