fix: improve error message for incorrect component types (#9066)

* Update error statement


* Add a new test

---------

Co-authored-by: Sebastian Husch Lee <sjrl@users.noreply.github.com>
This commit is contained in:
Amna Mubashar 2025-03-20 17:23:57 +05:00 committed by GitHub
parent db50579bbf
commit 833109900c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 4 deletions

View File

@ -197,12 +197,15 @@ class PipelineBase:
# ...then try again
if component_data["type"] not in component.registry:
raise PipelineError(
f"Successfully imported module {module} but can't find it in the component registry."
"This is unexpected and most likely a bug."
f"Successfully imported module '{module}' but couldn't find "
f"'{component_data['type']}' in the component registry.\n"
f"The component might be registered under a different path. "
f"Here are the registered components:\n {list(component.registry.keys())}\n"
)
except (ImportError, PipelineError, ValueError) as e:
raise PipelineError(
f"Component '{component_data['type']}' (name: '{name}') not imported."
f"Component '{component_data['type']}' (name: '{name}') not imported. Please "
f"check that the package is installed and the component path is correct."
) from e
# Create a new one

View File

@ -606,7 +606,30 @@ class TestPipelineBase:
with pytest.raises(PipelineError) as err:
PipelineBase.from_dict(data)
err.match(r"Component '' \(name: 'add_two'\) not imported.")
err.match(
r"Component '' \(name: 'add_two'\) not imported. Please check that the package is installed and the component path is correct."
)
def test_from_dict_with_correct_import_but_invalid_type(self):
# Test case: Module imports but component not found in registry.
data_registry_error = {
"metadata": {"test": "test"},
"components": {"add_two": {"type": "haystack.testing.NonExistentComponent", "init_parameters": {"add": 2}}},
"connections": [],
}
# Patch thread_safe_import so it doesn't raise an ImportError.
with patch("haystack.utils.type_serialization.thread_safe_import") as mock_import:
mock_import.return_value = None
with pytest.raises(PipelineError) as err_info:
PipelineBase.from_dict(data_registry_error)
outer_message = str(err_info.value)
inner_message = str(err_info.value.__cause__)
assert "Component 'haystack.testing.NonExistentComponent' (name: 'add_two') not imported." in outer_message
assert "Successfully imported module 'haystack.testing' but couldn't find" in inner_message
assert "in the component registry." in inner_message
assert "registered under a different path." in inner_message
# UNIT
def test_from_dict_without_connection_sender(self):