💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Fix Graph.insert() node id desync when a document's text/object field is None
chuenchen30910a6b233042e
diff --git a/src/python/txtai/graph/base.py b/src/python/txtai/graph/base.pyindex 30449d9d..8262e74d 100644--- a/src/python/txtai/graph/base.py+++ b/src/python/txtai/graph/base.py@@ -409,6 +409,12 @@ class Graph:# Manually provided relationships and attributes to copyrelations, attributes = None, {}+ # Tracks whether this document consumes an indexid slot. Must match the same+ # key-presence criterion Transform.stream() uses to compute its offset, so node+ # ids stay in sync with the real vector index even when a text/object field is+ # present but set to None.+ hasfield = True+# Extract data from dictionaryif isinstance(document, dict):# Extract relationships@@ -423,18 +429,20 @@ class Graph:}# Require text or object field+ hasfield = self.text in document or self.object in documentdocument = document.get(self.text, document.get(self.object))- if document is not None:- if isinstance(document, list):- # Join tokens as text- document = " ".join(document)+ if hasfield:+ if document is not None:+ if isinstance(document, list):+ # Join tokens as text+ document = " ".join(document)- # Create node- nodes.append((index, {**{"id": uid, "data": document}, **attributes}))+ # Create node+ nodes.append((index, {**{"id": uid, "data": document}, **attributes}))- # Add relationships- self.addrelations(index, relations)+ # Add relationships+ self.addrelations(index, relations)index += 1diff --git a/test/python/testgraph.py b/test/python/testgraph.pyindex 62d311b9..59c0f1fa 100644--- a/test/python/testgraph.py+++ b/test/python/testgraph.py@@ -188,6 +188,30 @@ class TestGraph(unittest.TestCase):self.assertTrue(graph.hasedge(0))self.assertTrue(graph.hasedge(0, 1))+ def testInsertNoneTextIndexSync(self):+ """+ Test that a document with a present but None text/object field consumes an+ indexid slot without creating a node, keeping later node ids in sync with+ the real vector index offset (which increments on key presence, not on the+ resolved value being non-None - see Transform.stream()).+ """++ graph = GraphFactory.create({})+ graph.initialize()++ documents = [+ ("d0", {"text": "alpha content"}, None),+ ("d1", {"text": None}, None),+ ("d2", {"text": "gamma content"}, None),+ ]+ graph.insert(documents, index=0)++ # d1's slot (index 1) is consumed but has no node - d2 must land at index 2, not 1+ self.assertTrue(graph.hasnode(0))+ self.assertFalse(graph.hasnode(1))+ self.assertTrue(graph.hasnode(2))+ self.assertEqual(graph.node(2)["id"], "d2")+def testFilter(self):"""Test creating filtered subgraphs