Skip to content

Commit 3119ae1

Browse files
tstadelanakin87
andauthored
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 <[email protected]> * Update test/core/pipeline/test_pipeline.py Co-authored-by: Stefano Fiorucci <[email protected]> * fix reno * fix reno * last reno fix --------- Co-authored-by: Stefano Fiorucci <[email protected]>
1 parent bf79f04 commit 3119ae1

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Diff for: haystack/core/pipeline/base.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ def from_dict(
167167
f"Successfully imported module {module} but can't find it in the component registry."
168168
"This is unexpected and most likely a bug."
169169
)
170-
except (ImportError, PipelineError) as e:
171-
raise PipelineError(f"Component '{component_data['type']}' not imported.") from e
170+
except (ImportError, PipelineError, ValueError) as e:
171+
raise PipelineError(
172+
f"Component '{component_data['type']}' (name: '{name}') not imported."
173+
) from e
172174

173175
# Create a new one
174176
component_class = component.registry[component_data["type"]]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
enhancements:
3+
- |
4+
When `Pipeline.from_dict` receives an invalid type (e.g. empty string), an informative `PipelineError` is now
5+
raised.

Diff for: test/core/pipeline/test_pipeline.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ def test_from_dict_without_component_type(self):
564564
err.match("Missing 'type' in component 'add_two'")
565565

566566
# UNIT
567-
def test_from_dict_without_registered_component_type(self, request):
567+
def test_from_dict_without_registered_component_type(self):
568568
data = {
569569
"metadata": {"test": "test"},
570570
"components": {"add_two": {"type": "foo.bar.baz", "init_parameters": {"add": 2}}},
@@ -575,6 +575,17 @@ def test_from_dict_without_registered_component_type(self, request):
575575

576576
err.match(r"Component .+ not imported.")
577577

578+
def test_from_dict_with_invalid_type(self):
579+
data = {
580+
"metadata": {"test": "test"},
581+
"components": {"add_two": {"type": "", "init_parameters": {"add": 2}}},
582+
"connections": [],
583+
}
584+
with pytest.raises(PipelineError) as err:
585+
Pipeline.from_dict(data)
586+
587+
err.match(r"Component '' \(name: 'add_two'\) not imported.")
588+
578589
# UNIT
579590
def test_from_dict_without_connection_sender(self):
580591
data = {"metadata": {"test": "test"}, "components": {}, "connections": [{"receiver": "some.receiver"}]}

0 commit comments

Comments
 (0)