diff --git a/haystack/core/component/types.py b/haystack/core/component/types.py index 4c949dad38..db4ddb917c 100644 --- a/haystack/core/component/types.py +++ b/haystack/core/component/types.py @@ -97,7 +97,17 @@ def __post_init__(self) -> None: # alias for Iterable[int]. Since we're interested in getting the inner type `int`, we call `get_args` # twice: the first time to get `list[int]` out of `Variadic`, the second time to get `int` out of `list[int]`. if self.is_lazy_variadic or self.is_greedy: - self.type = get_args(get_args(self.type)[0])[0] + outer_args = get_args(self.type) + inner_type = outer_args[0] + inner_args = get_args(inner_type) + if not inner_args: + from haystack.core.errors import ComponentError + + raise ComponentError( + f"Variadic input '{self.name}' must have a type argument, e.g. Variadic[int]. " + f"Got bare {inner_type!r} without a type argument." + ) + self.type = inner_args[0] class InputSocketTypeDescriptor(TypedDict): diff --git a/releasenotes/notes/fix-variadic-unsubscripted-indexerror-32788f5484bd3b5c.yaml b/releasenotes/notes/fix-variadic-unsubscripted-indexerror-32788f5484bd3b5c.yaml new file mode 100644 index 0000000000..13c638636f --- /dev/null +++ b/releasenotes/notes/fix-variadic-unsubscripted-indexerror-32788f5484bd3b5c.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fix ``InputSocket.__post_init__`` crashing with ``IndexError`` when a Variadic input + uses a bare ``Iterable`` without a type argument (e.g. ``Annotated[Iterable, HAYSTACK_VARIADIC_ANNOTATION]``). + Now raises ``ComponentError`` with a clear message instead of an unhelpful ``IndexError``. diff --git a/test/core/component/test_sockets.py b/test/core/component/test_sockets.py index 2892487120..e4fafe91ba 100644 --- a/test/core/component/test_sockets.py +++ b/test/core/component/test_sockets.py @@ -85,3 +85,17 @@ def test_contains(self): assert "input_1" in io assert "input_2" in io assert "invalid" not in io + + +def test_input_socket_variadic_without_type_arg_raises_component_error(): + """Bare Variadic (no type argument) should raise ComponentError, not IndexError.""" + from collections.abc import Iterable + from typing import Annotated + + from haystack.core.component.types import HAYSTACK_VARIADIC_ANNOTATION + from haystack.core.errors import ComponentError + + bare_variadic = Annotated[Iterable, HAYSTACK_VARIADIC_ANNOTATION] + + with pytest.raises(ComponentError, match="must have a type argument"): + InputSocket("inputs", bare_variadic)