Skip to content

Hypergraph is more powerful. Transform unstructured text into structured knowledge with LLMs. Graphs, hypergraphs, and spatio-temporal extractions — with one command.

test: cover batch merge with an empty first chunk

dex0shubham9eb414470931
diff --git a/tests/types/test_graph_dangling.py b/tests/types/test_graph_dangling.py
index 164af64..c180c0f 100644
--- a/tests/types/test_graph_dangling.py
+++ b/tests/types/test_graph_dangling.py
@@ -72,3 +72,34 @@ class TestAutoGraphMerge:
])
assert len(graph.nodes) == 1
+
+ def test_merge_batch_data_empty_first_chunk(self, llm_client, embedder):
+ """merge_batch_data tolerates a chunk that produced no nodes/edges.
+
+ Previously the first sublist being empty raised IndexError via
+ nodes_lists[0][0] / edges_lists[0][0].
+ """
+ graph = AutoGraph(
+ node_schema=Entity,
+ edge_schema=Relation,
+ node_key_extractor=lambda x: x.name,
+ edge_key_extractor=lambda x: f"{x.source}-{x.relation_type}-{x.target}",
+ nodes_in_edge_extractor=lambda x: (x.source, x.target),
+ llm_client=llm_client,
+ embedder=embedder,
+ )
+
+ # First chunk produced nothing; the second carries the real data.
+ nodes_lists = [
+ [],
+ [Entity(name="Apple", type="ORGANIZATION", properties={})],
+ ]
+ edges_lists = [
+ [],
+ [Relation(source="Apple", target="Steve", relation_type="founded_by")],
+ ]
+
+ result = graph.merge_batch_data((nodes_lists, edges_lists))
+
+ assert {n.name for n in result.nodes} == {"Apple"}
+ assert len(result.edges) == 1
diff --git a/tests/types/test_hypergraph_merge.py b/tests/types/test_hypergraph_merge.py
new file mode 100644
index 0000000..dce4343
--- /dev/null
+++ b/tests/types/test_hypergraph_merge.py
@@ -0,0 +1,60 @@
+"""Unit tests for AutoHypergraph batch merge."""
+
+from typing import List
+
+from pydantic import BaseModel, Field
+
+from hyperextract.types import AutoHypergraph
+
+
+class Entity(BaseModel):
+ """Simple node schema for testing."""
+
+ name: str
+ type: str
+ properties: dict = Field(default_factory=dict)
+
+
+class HyperRelation(BaseModel):
+ """Simple hyperedge schema for testing."""
+
+ participants: List[str]
+ relation_type: str
+
+
+class TestAutoHypergraphMerge:
+ """Test cases for AutoHypergraph merge functionality."""
+
+ def _make_graph(self, llm_client, embedder):
+ return AutoHypergraph(
+ node_schema=Entity,
+ edge_schema=HyperRelation,
+ node_key_extractor=lambda x: x.name,
+ edge_key_extractor=lambda x: f"{x.relation_type}_{sorted(x.participants)}",
+ nodes_in_edge_extractor=lambda x: tuple(x.participants),
+ llm_client=llm_client,
+ embedder=embedder,
+ )
+
+ def test_merge_batch_data_empty_first_chunk(self, llm_client, embedder):
+ """merge_batch_data tolerates a chunk that produced no nodes/edges.
+
+ Previously the first sublist being empty raised IndexError via
+ nodes_lists[0][0] / edges_lists[0][0].
+ """
+ graph = self._make_graph(llm_client, embedder)
+
+ # First chunk produced nothing; the second carries the real data.
+ nodes_lists = [
+ [],
+ [Entity(name="Apple", type="ORGANIZATION", properties={})],
+ ]
+ edges_lists = [
+ [],
+ [HyperRelation(participants=["Apple", "Steve"], relation_type="founded_by")],
+ ]
+
+ result = graph.merge_batch_data((nodes_lists, edges_lists))
+
+ assert {n.name for n in result.nodes} == {"Apple"}
+ assert len(result.edges) == 1