Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: raise PipelineError when Pipeline.from_dict receives an invalid type #8711

Merged
merged 8 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading