diff --git a/semantica/context/context_graph.py b/semantica/context/context_graph.py index 6a095c69..d18c2b54 100644 --- a/semantica/context/context_graph.py +++ b/semantica/context/context_graph.py @@ -2764,8 +2764,11 @@ class ContextGraph: """ # Normalize so callers may use either vocabulary's spelling # ("causes" from CausalChainAnalyzer, or "CAUSED" from this module's - # canonical constant); the stored form is always canonical. - relationship_type = _CAUSAL_EDGE_ALIASES.get(relationship_type.upper()) + # canonical constant); the stored form is always canonical. Invalid + # inputs keep raising ValueError rather than AttributeError. + if not isinstance(relationship_type, str): + raise ValueError(f"Relationship type must be one of: {_CAUSAL_EDGE_TYPES}") + relationship_type = _CAUSAL_EDGE_ALIASES.get(relationship_type.strip().upper()) if relationship_type is None: raise ValueError(f"Relationship type must be one of: {_CAUSAL_EDGE_TYPES}") diff --git a/tests/context/test_decision_causal_edge_regression.py b/tests/context/test_decision_causal_edge_regression.py index ed411251..211864d0 100644 --- a/tests/context/test_decision_causal_edge_regression.py +++ b/tests/context/test_decision_causal_edge_regression.py @@ -7,6 +7,8 @@ extraction found nothing, the chain came back empty even though an explicit ``CAUSED`` edge was stored in the graph. """ +import pytest + from semantica.context import ContextGraph from semantica.context.context_graph import ContextEdge @@ -371,3 +373,21 @@ def test_add_causal_relationship_accepts_any_case_and_stores_canonical(): ] assert edges, "add_causal_relationship must store the edge" assert edges[0].edge_type == "CAUSED" + + +def test_add_causal_relationship_rejects_non_string_with_value_error(): + """Invalid relationship types must keep raising ValueError (issue #1184 + follow-up): normalization must not turn them into AttributeError.""" + graph = ContextGraph(advanced_analytics=True) + cause = graph.record_decision( + category="a", scenario="upstream", reasoning="r", + outcome="x", confidence=0.9, + ) + effect = graph.record_decision( + category="b", scenario="downstream", reasoning="r", + outcome="y", confidence=0.9, + ) + + for bad_type in (None, 42, ["CAUSED"]): + with pytest.raises(ValueError): + graph.add_causal_relationship(cause, effect, relationship_type=bad_type)