Skip to content

Commit

Permalink
refactor: raise PipelineError when Pipeline.from_dict receives an…
Browse files Browse the repository at this point in the history
… 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]>
  • Loading branch information
tstadel and anakin87 authored Jan 23, 2025
1 parent bf79f04 commit 3119ae1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
6 changes: 4 additions & 2 deletions haystack/core/pipeline/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ def from_dict(
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"]]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
enhancements:
- |
When `Pipeline.from_dict` receives an invalid type (e.g. empty string), an informative `PipelineError` is now
raised.
13 changes: 12 additions & 1 deletion test/core/pipeline/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def test_from_dict_without_component_type(self):
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}}},
Expand All @@ -575,6 +575,17 @@ def test_from_dict_without_registered_component_type(self, request):

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"}]}
Expand Down

0 comments on commit 3119ae1

Please sign in to comment.