Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(ner): discard unresolved merge spans

Inference_058527bf4a96
diff --git a/semantica/semantic_extract/ner_extractor.py b/semantica/semantic_extract/ner_extractor.py
index 14dd7e16..242a0f9f 100644
--- a/semantica/semantic_extract/ner_extractor.py
+++ b/semantica/semantic_extract/ner_extractor.py
@@ -368,9 +368,10 @@ class NERExtractor:
Some providers, notably typed LLM extraction, can return text and
labels without offsets. For a single method that is harmless, but a
- consensus merge needs document locations. Missing spans are therefore
+ span-based merge needs document locations. Missing spans are therefore
aligned by a deterministic, per-label text search. Valid provider
- offsets are preserved and input entities are never mutated.
+ offsets are preserved; candidates that cannot be aligned are excluded
+ because union and consensus cannot safely merge them.
"""
next_offsets = {}
occupied_offsets = {}
@@ -378,8 +379,7 @@ class NERExtractor:
for entity in entities:
needle = entity.text
- if not needle:
- aligned.append(entity)
+ if not isinstance(needle, str) or not needle:
continue
key = (needle.casefold(), self._canonical_label(entity.label))
@@ -425,7 +425,6 @@ class NERExtractor:
break
if match is None:
- aligned.append(entity)
continue
resolved_start = match_offset + match.start()
diff --git a/tests/semantic_extract/test_ner_merge_strategies.py b/tests/semantic_extract/test_ner_merge_strategies.py
index da15bfeb..cd00257c 100644
--- a/tests/semantic_extract/test_ner_merge_strategies.py
+++ b/tests/semantic_extract/test_ner_merge_strategies.py
@@ -482,6 +482,37 @@ def test_extract_consensus_recovers_missing_llm_offsets_before_voting():
assert result[0].metadata["supporting_methods"] == ["llm", "regex"]
[email protected]("start, end", [(0, 0), (None, None)])
+def test_union_discards_unresolved_invalid_offsets(start, end):
+ """Unresolvable default or nullable spans cannot leak into union output."""
+ extractor = NERExtractor(method="llm", merge_strategy="union")
+ responses = {"llm": [entity("Absent", "ORG", start, end, 0.9)]}
+
+ with patch(
+ "semantica.semantic_extract.methods.get_entity_method",
+ side_effect=lambda method_name: lambda _text, **_options: responses[
+ method_name
+ ],
+ ):
+ assert extractor.extract_entities("Apple") == []
+
+
+def test_union_recovers_nullable_offsets_when_text_matches():
+ """Nullable custom offsets remain usable when they can be aligned safely."""
+ extractor = NERExtractor(method="custom", merge_strategy="union")
+ responses = {"custom": [entity("Apple", "ORG", None, None, 0.9)]}
+
+ with patch(
+ "semantica.semantic_extract.methods.get_entity_method",
+ side_effect=lambda method_name: lambda _text, **_options: responses[
+ method_name
+ ],
+ ):
+ result = extractor.extract_entities("Apple")
+
+ assert [(item.start_char, item.end_char) for item in result] == [(0, 5)]
+
+
def test_missing_offsets_are_assigned_to_distinct_repeated_mentions():
"""Text-only duplicate output is aligned in occurrence order before merging."""
extractor = NERExtractor(method="llm", merge_strategy="union")