Hypergraph is more powerful. Transform unstructured text into structured knowledge with LLMs. Graphs, hypergraphs, and spatio-temporal extractions — with one command.
fix: raise ValueError for unknown TemplateFactory types
Frankie-Xub917df9d4480
diff --git a/hyperextract/utils/template_engine/factory.py b/hyperextract/utils/template_engine/factory.pyindex 77e43ca..050f6ed 100644--- a/hyperextract/utils/template_engine/factory.py+++ b/hyperextract/utils/template_engine/factory.py@@ -1,10 +1,12 @@"""Template Factory - Dynamically creates template instances from configuration.-Supports all 8 AutoType dynamic generation.+`create()` currently wires: model, list, set, graph, hypergraph,+temporal_graph, spatial_graph, spatio_temporal_graph.+Unknown `type` values raise ValueError (document is not wired here)."""from pathlib import Path-from typing import TYPE_CHECKING+from typing import TYPE_CHECKING, get_argsfrom langchain_core.embeddings import Embeddingsfrom langchain_core.language_models import BaseChatModel@@ -18,6 +20,7 @@ from .parsers import (parse_option,parse_output,)+from .parsers.schemas.base import VALID_AUTOTYPESif TYPE_CHECKING:from hyperextract.types import (@@ -32,6 +35,11 @@ if TYPE_CHECKING:)+def _unknown_autotype_error(type_name: str) -> ValueError:+ allowed = ", ".join(get_args(VALID_AUTOTYPES))+ return ValueError(f"Unknown template type {type_name!r}. Allowed types: {allowed}")++class TemplateFactory:@classmethoddef create_method(@@ -462,6 +470,9 @@ class TemplateFactory:if template_cfg is None:raise ValueError(f"Template not found: {source}")+ if template_cfg.type not in get_args(VALID_AUTOTYPES):+ raise _unknown_autotype_error(template_cfg.type)+template_cfg = localize_template(template_cfg, language)match template_cfg.type:@@ -493,6 +504,8 @@ class TemplateFactory:template = cls.create_spatio_temporal_graph(template_cfg, llm_client, embedder, **kwargs)+ case _:+ raise _unknown_autotype_error(template_cfg.type)if isinstance(source, str):template.metadata["template"] = (diff --git a/tests/template_engine/test_factory.py b/tests/template_engine/test_factory.pyindex fb79318..15bc7a2 100644--- a/tests/template_engine/test_factory.py+++ b/tests/template_engine/test_factory.py@@ -1,6 +1,9 @@"""Unit tests for TemplateFactory."""+from pathlib import Path+import pytest+import yamlfrom hyperextract.utils.template_engine import Gallery, TemplateFactory@@ -125,3 +128,35 @@ class TestTemplateFactoryCreateAllTypes:assert isinstance(result, AutoGraph)assert result.metadata.get("type") == "graph"+++class TestTemplateFactoryUnknownType:+ """Unknown AutoType must fail closed with ValueError, not UnboundLocalError."""++ def test_unknown_type_yaml_raises_valueerror(self, tmp_path, llm_client, embedder):+ repo_root = Path(__file__).resolve().parents[2]+ src = (repo_root / "hyperextract/templates/presets/general/base_model.yaml").read_text(+ encoding="utf-8"+ )+ yaml_path = tmp_path / "not_a_type.yaml"+ yaml_path.write_text(+ src.replace("\ntype: model\n", "\ntype: not_a_type\n"),+ encoding="utf-8",+ )+ raw_type = yaml.safe_load(yaml_path.read_text(encoding="utf-8"))["type"]+ assert raw_type == "not_a_type"++ # Parser Literal rejects unknown types at load; assignment reaches the+ # factory match the same way an unbound type would after a looser schema.+ cfg = Gallery.get("general/model").model_copy(deep=True)+ cfg.type = raw_type++ with pytest.raises(ValueError, match="not_a_type") as exc_info:+ TemplateFactory.create(cfg, "en", llm_client, embedder)++ assert not isinstance(exc_info.value, UnboundLocalError)+ message = str(exc_info.value)+ assert "Allowed types:" in message+ assert "model" in message+ assert "graph" in message+ assert "hypergraph" in message