Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(ontology): stop inferring framework entity fields as datatype properties (#1420)

Kevin6b8437781e0c
diff --git a/semantica/ontology/property_generator.py b/semantica/ontology/property_generator.py
index f90bf739..28585e77 100644
--- a/semantica/ontology/property_generator.py
+++ b/semantica/ontology/property_generator.py
@@ -37,6 +37,29 @@ from .naming_conventions import NamingConventions
from .relationship_utils import build_entity_aliases, resolve_relationship_endpoint_type
+# Top-level entity keys that describe structure or provenance rather than
+# business attributes. GraphBuilder and EntityMerger attach these to entity
+# dicts (relationships list, nested properties/metadata maps, merge history),
+# so they must not be inferred as datatype properties. Each key mirrors what
+# the framework actually writes to a merged entity top level
+# (see MergeStrategyManager._merge_entities merged_entity dict and GraphBuilder).
+_CONTROL_FIELDS = frozenset(
+ {
+ "id",
+ "type",
+ "entity_type",
+ "text",
+ "label",
+ "confidence",
+ "properties",
+ "relationships",
+ "metadata",
+ "merged_from",
+ "merge_strategy",
+ }
+)
+
+
class PropertyGenerator:
"""
Property generation engine for ontologies.
@@ -347,7 +370,7 @@ class PropertyGenerator:
for entity in entities:
for key, value in entity.items():
- if key in ["id", "type", "entity_type", "text", "label", "confidence"]:
+ if key in _CONTROL_FIELDS:
continue
# Infer type
diff --git a/tests/ontology/test_ontology_framework_fields.py b/tests/ontology/test_ontology_framework_fields.py
new file mode 100644
index 00000000..2defe5c3
--- /dev/null
+++ b/tests/ontology/test_ontology_framework_fields.py
@@ -0,0 +1,94 @@
+"""Framework/control entity keys must not be inferred as datatype properties.
+
+The _CONTROL_FIELDS skip set mirrors exactly what the framework writes to a
+merged entity's top level (see MergeStrategyManager._merge_entities): no extra
+guesses, so business attributes that merely share a common name (e.g. source)
+keep getting inferred.
+"""
+
+from semantica.deduplication.merge_strategy import MergeStrategyManager
+from semantica.ontology.property_generator import PropertyGenerator
+
+
+def _merged_entity():
+ """Run a real merge so the entity carries the framework's actual top-level keys."""
+ manager = MergeStrategyManager(default_strategy="keep_most_complete")
+ result = manager.merge_entities(
+ [
+ {"id": "b1", "name": "Hangzhou Branch", "type": "ORG", "employee_count": 120},
+ {"id": "b2", "name": "Hangzhou Branch", "type": "ORG"},
+ ]
+ )
+ return result.merged_entity
+
+
+def test_framework_fields_not_inferred_as_data_properties():
+ entity = _merged_entity()
+ classes = [{"name": "Organization", "metadata": {"inferred_from": "ORG"}}]
+
+ properties = PropertyGenerator().infer_properties([entity], [], classes)
+
+ names = {p["name"] for p in properties}
+ for framed in (
+ "properties",
+ "relationships",
+ "metadata",
+ "merged_from",
+ "merge_strategy",
+ ):
+ assert framed not in names, f"framework field {framed} leaked as a property"
+
+
+def test_business_attributes_still_inferred():
+ entity = _merged_entity()
+ classes = [{"name": "Organization", "metadata": {"inferred_from": "ORG"}}]
+
+ properties = PropertyGenerator().infer_properties([entity], [], classes)
+
+ names = {p["name"] for p in properties}
+ assert "name" in names
+ assert "metadata" not in names
+
+
+def test_source_field_still_inferred_as_business_attribute():
+ """A top-level 'source' is a business attribute, not a framework field."""
+ entity = {
+ "id": "b1",
+ "name": "Hangzhou Branch",
+ "type": "ORG",
+ "source": "doc-42",
+ }
+ classes = [{"name": "Organization", "metadata": {"inferred_from": "ORG"}}]
+
+ properties = PropertyGenerator().infer_properties([entity], [], classes)
+
+ names = {p["name"] for p in properties}
+ assert "source" in names
+
+
+def test_unmerged_graphbuilder_entities_infer_business_attributes():
+ """Flat entities from GraphBuilder (merge_entities=False) must not lose business
+ attributes through _CONTROL_FIELDS: name and domain-specific fields must be
+ inferred, and none of the framework keys should appear in the output."""
+ from semantica.kg.graph_builder import GraphBuilder
+
+ builder = GraphBuilder(merge_entities=False, resolve_conflicts=False)
+ graph = builder.build(
+ {
+ "entities": [
+ {"id": "c1", "name": "Chengdu Plant", "type": "ORG", "headcount": 300},
+ {"id": "c2", "name": "Wuhan Plant", "type": "ORG", "headcount": 450},
+ ],
+ "relationships": [],
+ }
+ )
+ entities = graph["entities"]
+ classes = [{"name": "Organization", "metadata": {"inferred_from": "ORG"}}]
+
+ properties = PropertyGenerator().infer_properties(entities, [], classes)
+
+ names = {p["name"] for p in properties}
+ assert "name" in names, "name must be inferred from flat GraphBuilder entities"
+ assert "headcount" in names, "domain business attribute must be inferred"
+ for framed in ("properties", "relationships", "metadata", "merged_from", "merge_strategy"):
+ assert framed not in names, f"framework field {framed!r} must not appear"