Skip to content
Open
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
12 changes: 11 additions & 1 deletion haystack/core/component/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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``.
14 changes: 14 additions & 0 deletions test/core/component/test_sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)