Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
fix(kg): extract from FileObject sources and warn on unsupported ones (#1951)
Tymure7f2f5a5ab4d
diff --git a/semantica/kg/graph_builder.py b/semantica/kg/graph_builder.pyindex a0b35924..0b47e84d 100644--- a/semantica/kg/graph_builder.py+++ b/semantica/kg/graph_builder.py@@ -175,6 +175,23 @@ class GraphBuilder:def _process_item(self, item: Any, all_entities: List[Any], all_relationships: List[Any], **options):"""Helper to process a single item and add to entities or relationships list."""+ from ..ingest.file_ingestor import FileObject++ if isinstance(item, FileObject):+ content = item.content or ""+ if isinstance(content, bytes):+ # Binary formats (images, PDF, Office) need a parser, not raw decoding.+ try:+ content = content.decode("utf-8")+ except UnicodeDecodeError:+ self.logger.warning(+ "Skipping FileObject %r: %s content is not UTF-8 text",+ item.name,+ item.file_type,+ )+ return+ item = content+if isinstance(item, str):# Treat string as text for extractionself._extract_from_text(item, all_entities, all_relationships, **options)@@ -342,7 +359,9 @@ class GraphBuilder:found_something = Trueelse:# Unknown type- pass+ self.logger.warning(+ "Skipping unsupported source type: %s", type(item).__name__+ )def _get_extractor(self, kind: str, extractor_cls, method: Union[str, List[str]]diff --git a/tests/kg/test_graph_builder_extraction_defaults.py b/tests/kg/test_graph_builder_extraction_defaults.pyindex fdc2a0e2..ab016dce 100644--- a/tests/kg/test_graph_builder_extraction_defaults.py+++ b/tests/kg/test_graph_builder_extraction_defaults.py@@ -9,6 +9,7 @@ key for any raw-text build.import unittestfrom unittest.mock import patch+from semantica.ingest.file_ingestor import FileObjectfrom semantica.kg.graph_builder import GraphBuilder@@ -258,5 +259,62 @@ class TestGraphBuilderDefaultsRunOffline(unittest.TestCase):self.assertIsInstance(entities, list)+class TestGraphBuilderFileObjectSources(unittest.TestCase):+ def setUp(self):+ self.builder = GraphBuilder(merge_entities=False, resolve_conflicts=False)++ def test_file_object_extracts_same_graph_as_text(self):+ text = "Apple Inc. was founded by Steve Jobs in 1976."+ source = FileObject(+ path="apple.txt",+ name="apple.txt",+ size=len(text),+ file_type="txt",+ content=text.encode("utf-8"),+ )++ expected = self.builder.build([text], ner_method="pattern")+ actual = self.builder.build([source], ner_method="pattern")++ self.assertTrue(expected["entities"])+ self.assertTrue(expected["relationships"])+ self.assertEqual(actual["entities"], expected["entities"])+ self.assertEqual(actual["relationships"], expected["relationships"])++ def test_binary_file_object_warns_and_is_skipped(self):+ text = b"Apple Inc. was founded by Steve Jobs in 1976."+ for name, content in (+ ("pixel.png", b"\x89PNG\r\n\x1a\n" + text),+ ("paper.pdf", b"%PDF-1.4\n%\xe2\xe3\xcf\xd3\n" + text),+ ):+ with self.subTest(name=name):+ source = FileObject(+ path=name,+ name=name,+ size=len(content),+ file_type=name.rsplit(".", 1)[1],+ content=content,+ )+ entity = {"id": "known", "name": "Known"}+ with self.assertLogs(self.builder.logger, level="WARNING") as logs:+ graph = self.builder.build([source, entity], ner_method="pattern")++ self.assertTrue(any(name in message for message in logs.output))+ self.assertEqual(graph["entities"], [entity])+ self.assertEqual(graph["relationships"], [])++ def test_unknown_source_warns_and_build_continues(self):+ class UnsupportedSource:+ pass++ entity = {"id": "known", "name": "Known"}+ with self.assertLogs(self.builder.logger, level="WARNING") as logs:+ graph = self.builder.build([UnsupportedSource(), entity])++ self.assertTrue(any("UnsupportedSource" in message for message in logs.output))+ self.assertEqual(graph["entities"], [entity])+ self.assertEqual(graph["relationships"], [])++if __name__ == "__main__":unittest.main()