Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(crewai): pass NER entities to RelationExtractor in the extract_relations action (#1853)

Tymuref2491625165
diff --git a/integrations/crewai/kg_tool.py b/integrations/crewai/kg_tool.py
index b7310bb9..5c0d657f 100644
--- a/integrations/crewai/kg_tool.py
+++ b/integrations/crewai/kg_tool.py
@@ -367,7 +367,12 @@ class SemanticaKGTool(_BaseTool): # type: ignore[misc]
def _extract_relations(self, text: str) -> str:
"""Extract relationships between entities in ``text``."""
try:
- raw = self.relation_extractor.extract_relations(text) or []
+ # RelationExtractor.extract_relations() requires the entities to
+ # relate, so run NER first, as _add_from_text() does (#1849).
+ entities = self.ner_extractor.extract_entities(text) or []
+ raw = (
+ self.relation_extractor.extract_relations(text, entities=entities) or []
+ )
relations = [
{
"source": self._relation_source(r),
diff --git a/tests/integrations/crewai/test_kg_tool.py b/tests/integrations/crewai/test_kg_tool.py
index 18ee1eda..69c49b26 100644
--- a/tests/integrations/crewai/test_kg_tool.py
+++ b/tests/integrations/crewai/test_kg_tool.py
@@ -402,6 +402,20 @@ class TestSemanticaKGToolDataclassShapes(unittest.TestCase):
self.assertEqual(result["relations"][0]["relation"], "FOUNDED_BY")
self.assertEqual(result["relations"][0]["target"], "Elon Musk")
+ def test_extract_relations_passes_entity_objects_to_relation_extractor(self):
+ """RelationExtractor needs the NER entities; without them the real
+ extractor raises TypeError (#1849)."""
+ from semantica.semantic_extract.types import Entity
+
+ self.tool._run(action="extract_relations", text="Tesla founded by Elon Musk")
+
+ self.assertIsNotNone(self.rel.received_entities)
+ self.assertEqual(
+ [e.text for e in self.rel.received_entities], ["Tesla", "Elon Musk"]
+ )
+ for e in self.rel.received_entities:
+ self.assertIsInstance(e, Entity)
+
def test_add_to_graph_passes_entity_objects_to_relation_extractor(self):
result = json.loads(
self.tool._run(action="add_to_graph", text="Tesla founded by Elon Musk")