Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(kg): validate chunk_size in chunked label propagation (#1938)

Thomas Lin4b1611b49f62
diff --git a/semantica/kg/community_detector.py b/semantica/kg/community_detector.py
index a6bed516..f20055a3 100644
--- a/semantica/kg/community_detector.py
+++ b/semantica/kg/community_detector.py
@@ -721,6 +721,13 @@ class CommunityDetector:
ValueError: If graph is empty or parameters are invalid
RuntimeError: If community detection fails
"""
+ if (
+ isinstance(chunk_size, bool)
+ or not isinstance(chunk_size, int)
+ or chunk_size < 1
+ ):
+ raise ValueError("chunk_size must be an integer at least 1")
+
try:
self.logger.info("Detecting communities using Label Propagation algorithm")
diff --git a/tests/kg/test_community_detector_chunk_size.py b/tests/kg/test_community_detector_chunk_size.py
new file mode 100644
index 00000000..fe9d9a78
--- /dev/null
+++ b/tests/kg/test_community_detector_chunk_size.py
@@ -0,0 +1,20 @@
+import pytest
+
+from semantica.kg import CommunityDetector
+
+
[email protected]("chunk_size", [0, -1, None, 1.5, True])
+def test_label_propagation_rejects_invalid_chunk_size(chunk_size):
+ graph = {
+ "nodes": [{"id": str(i)} for i in range(4)],
+ "edges": [{"source": "0", "target": "1"}],
+ }
+
+ with pytest.raises(
+ ValueError, match="chunk_size must be an integer at least 1"
+ ):
+ CommunityDetector().detect_communities(
+ graph,
+ algorithm="label_propagation",
+ chunk_size=chunk_size,
+ )