From 3119ae1ec9f0a310ea98f034d1e3963d2ae14975 Mon Sep 17 00:00:00 2001 From: tstadel <60758086+tstadel@users.noreply.github.com> Date: Thu, 23 Jan 2025 12:40:19 +0100 Subject: [PATCH] refactor: raise `PipelineError` when `Pipeline.from_dict` receives an invalid type (#8711) * fix: error on invalid type * add reno * Update releasenotes/notes/fix-invalid-component-type-error-83ee00d820b63cc5.yaml Co-authored-by: Stefano Fiorucci * Update test/core/pipeline/test_pipeline.py Co-authored-by: Stefano Fiorucci * fix reno * fix reno * last reno fix --------- Co-authored-by: Stefano Fiorucci --- haystack/core/pipeline/base.py | 6 ++++-- ...valid-component-type-error-83ee00d820b63cc5.yaml | 5 +++++ test/core/pipeline/test_pipeline.py | 13 ++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/fix-invalid-component-type-error-83ee00d820b63cc5.yaml diff --git a/haystack/core/pipeline/base.py b/haystack/core/pipeline/base.py index d8f2a6593..155ba27fe 100644 --- a/haystack/core/pipeline/base.py +++ b/haystack/core/pipeline/base.py @@ -167,8 +167,10 @@ class PipelineBase: f"Successfully imported module {module} but can't find it in the component registry." "This is unexpected and most likely a bug." ) - except (ImportError, PipelineError) as e: - raise PipelineError(f"Component '{component_data['type']}' not imported.") from e + except (ImportError, PipelineError, ValueError) as e: + raise PipelineError( + f"Component '{component_data['type']}' (name: '{name}') not imported." + ) from e # Create a new one component_class = component.registry[component_data["type"]] diff --git a/releasenotes/notes/fix-invalid-component-type-error-83ee00d820b63cc5.yaml b/releasenotes/notes/fix-invalid-component-type-error-83ee00d820b63cc5.yaml new file mode 100644 index 000000000..64119cfa8 --- /dev/null +++ b/releasenotes/notes/fix-invalid-component-type-error-83ee00d820b63cc5.yaml @@ -0,0 +1,5 @@ +--- +enhancements: + - | + When `Pipeline.from_dict` receives an invalid type (e.g. empty string), an informative `PipelineError` is now + raised. diff --git a/test/core/pipeline/test_pipeline.py b/test/core/pipeline/test_pipeline.py index 1cd57a5b5..1f0284301 100644 --- a/test/core/pipeline/test_pipeline.py +++ b/test/core/pipeline/test_pipeline.py @@ -564,7 +564,7 @@ class TestPipeline: err.match("Missing 'type' in component 'add_two'") # UNIT - def test_from_dict_without_registered_component_type(self, request): + def test_from_dict_without_registered_component_type(self): data = { "metadata": {"test": "test"}, "components": {"add_two": {"type": "foo.bar.baz", "init_parameters": {"add": 2}}}, @@ -575,6 +575,17 @@ class TestPipeline: err.match(r"Component .+ not imported.") + def test_from_dict_with_invalid_type(self): + data = { + "metadata": {"test": "test"}, + "components": {"add_two": {"type": "", "init_parameters": {"add": 2}}}, + "connections": [], + } + with pytest.raises(PipelineError) as err: + Pipeline.from_dict(data) + + err.match(r"Component '' \(name: 'add_two'\) not imported.") + # UNIT def test_from_dict_without_connection_sender(self): data = {"metadata": {"test": "test"}, "components": {}, "connections": [{"receiver": "some.receiver"}]}