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.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.py
index 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 copy
relations, 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 dictionary
if 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 document
document = 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 += 1
diff --git a/test/python/testgraph.py b/test/python/testgraph.py
index 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