diff --git a/haystack/core/type_utils.py b/haystack/core/type_utils.py index bef6ca15b60..92a83fafd89 100644 --- a/haystack/core/type_utils.py +++ b/haystack/core/type_utils.py @@ -165,6 +165,16 @@ def _strict_types_are_compatible(sender: Any, receiver: Any) -> bool: # noqa: P if (sender_origin is Union and not sender_args) or (receiver_origin is Union and not receiver_args): return False + # A Literal stands for its values: each one must match the receiver, either by its type or, when the receiver is + # a Union, as a member of one of its Literal arguments. Literal receivers are matched by membership below. + if sender_origin is Literal and receiver_origin is not Literal: + if receiver_origin is Union: + return all( + any(_strict_types_are_compatible(Literal[value], union_arg) for union_arg in receiver_args) + for value in sender_args + ) + return all(_strict_types_are_compatible(type(value), receiver) for value in sender_args) + if sender_origin is not Union and receiver_origin is Union: return any(_strict_types_are_compatible(sender, union_arg) for union_arg in receiver_args) diff --git a/releasenotes/notes/literal-output-connects-to-base-type-9df6b031d226599b.yaml b/releasenotes/notes/literal-output-connects-to-base-type-9df6b031d226599b.yaml new file mode 100644 index 00000000000..bf5d86b8d4e --- /dev/null +++ b/releasenotes/notes/literal-output-connects-to-base-type-9df6b031d226599b.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + ``Pipeline.connect()`` now accepts a ``Literal`` output connected to an input that takes the type of its values. + For example, an output declared as ``Literal["positive", "negative"]`` can feed a ``str`` input, and + ``Literal["a"]`` can feed ``str | None`` or ``list[str]``. Before, these connections raised a + ``PipelineConnectError`` even though every value the sender can produce is valid for the receiver. diff --git a/test/core/pipeline/test_pipeline.py b/test/core/pipeline/test_pipeline.py index abebf39d8b9..43f582d9f79 100644 --- a/test/core/pipeline/test_pipeline.py +++ b/test/core/pipeline/test_pipeline.py @@ -4,6 +4,7 @@ import logging from concurrent.futures import ThreadPoolExecutor +from typing import Literal import pytest @@ -551,3 +552,32 @@ def run(self, text: str) -> dict[str, str]: p.add_component("consumer", SingleStrConsumer()) p.connect("producer.texts", "consumer.text") assert p.run({}) == {"consumer": {"out": "only-one"}} + + def test_run_wraps_literal_output_into_list_input(self): + @component + class LiteralProducer: + @component.output_types(label=Literal["a", "b"]) + def run(self) -> dict[str, str]: + return {"label": "a"} + + p = Pipeline() + p.add_component("producer", LiteralProducer()) + p.add_component("receiver", ListStrAcceptor()) + p.connect("producer.label", "receiver.texts") + + assert p.run({}) == {"receiver": {"result": ["a"]}} + + @pytest.mark.asyncio + async def test_run_async_wraps_literal_output_into_list_input(self): + @component + class LiteralProducer: + @component.output_types(label=Literal["a", "b"]) + def run(self) -> dict[str, str]: + return {"label": "a"} + + p = Pipeline() + p.add_component("producer", LiteralProducer()) + p.add_component("receiver", ListStrAcceptor()) + p.connect("producer.label", "receiver.texts") + + assert await p.run_async({}) == {"receiver": {"result": ["a"]}} diff --git a/test/core/pipeline/test_pipeline_base.py b/test/core/pipeline/test_pipeline_base.py index 59a5c28c15b..e42b5276f96 100644 --- a/test/core/pipeline/test_pipeline_base.py +++ b/test/core/pipeline/test_pipeline_base.py @@ -4,7 +4,7 @@ import logging from collections.abc import Iterable -from typing import Any +from typing import Any, Literal from unittest.mock import patch import pytest @@ -2320,6 +2320,16 @@ def test_connect_list_output_to_iterable_input(self): assert list(pipe.graph.edges) == [("producer", "consumer", "items/sources")] assert pipe.graph["producer"]["consumer"]["items/sources"]["conversion_strategy"] is None + def test_connect_literal_output_to_str_input(self): + producer = component_class("Producer", output_types={"label": Literal["positive", "negative"]})() + consumer = component_class("Consumer", input_types={"text": str})() + pipe = PipelineBase() + pipe.add_component("producer", producer) + pipe.add_component("consumer", consumer) + pipe.connect("producer.label", "consumer.text") + assert list(pipe.graph.edges) == [("producer", "consumer", "label/text")] + assert pipe.graph["producer"]["consumer"]["label/text"]["conversion_strategy"] is None + def test_connect_list_output_to_list_or_iterable_input_is_ambiguous(self): producer = component_class("Producer", output_types={"value": list[str]})() consumer = component_class("Consumer", input_types={"list_items": list[str], "iterable_items": Iterable[str]})() diff --git a/test/core/test_type_utils.py b/test/core/test_type_utils.py index 6531b71b150..add58afabb2 100644 --- a/test/core/test_type_utils.py +++ b/test/core/test_type_utils.py @@ -181,6 +181,16 @@ def generate_strict_asymmetric_cases(): pytest.param(bool, int, id="different-primitives"), pytest.param((Literal["a", "b"]), (Literal["a", "b", "c"]), id="sending-subset-literal"), pytest.param((Literal["b", "c"]), (Literal["a", "b", "c"]), id="sending-non-prefix-subset-literal"), + pytest.param(Literal["a", "b"], str, id="string-literal-to-str"), + pytest.param(Literal[1, 2], int, id="int-literal-to-int"), + pytest.param(Literal[Enum1.TEST1], Enum1, id="enum-literal-to-enum"), + pytest.param(Literal["a"], str | None, id="string-literal-to-optional-str-pep604"), + pytest.param(Literal["a"], Optional[str], id="string-literal-to-optional-str"), + pytest.param(Literal[1, "a"], Union[int, str], id="mixed-literal-to-typing-union-of-value-types"), + pytest.param(Literal[None], str | None, id="none-literal-to-optional-str"), + pytest.param(Literal[1, "a"], int | str, id="mixed-literal-to-union-of-value-types"), + pytest.param(Literal["a", 1], Literal["a"] | int, id="mixed-literal-to-union-of-literal-and-type"), + pytest.param(list[Literal["a"]], list[str], id="list-of-string-literal-to-list-of-str"), pytest.param((Optional[str]), (Union[int, str, None]), id="union-to-superset-union-in-other-order"), pytest.param(str | None, str | int | None, id="union-to-superset-union-in-other-order-pep604"), pytest.param( @@ -554,6 +564,9 @@ def test_list_and_iterable_are_not_compatible_strict(sender_type, receiver_type) pytest.param((Literal["a", "b", "c"]), (Literal["x", "y"]), id="different-literal-of-same-primitive"), pytest.param((Literal["a", "x"]), (Literal["a", "b", "c"]), id="partially-overlapping-literals"), pytest.param((Literal[Enum1.TEST1]), (Literal[Enum1.TEST2]), id="different-literal-of-same-enum"), + pytest.param(Literal["a"], int, id="string-literal-to-different-primitive"), + pytest.param(Literal[1, "a"], int, id="mixed-literal-to-type-of-some-values"), + pytest.param(Literal["a", "x"], Literal["a"] | int, id="literal-partially-matching-union-of-literal-and-type"), pytest.param( (List[Set[Sequence[str]]]), (List[Set[Sequence[bool]]]), id="nested-sequences-of-different-primitives" ), @@ -1031,6 +1044,9 @@ def test_types_are_compatible_with_conversion(self): assert _types_are_compatible(sender=str, receiver=List[str]) == (True, ConversionStrategy.WRAP) assert _types_are_compatible(sender=str, receiver=list[str]) == (True, ConversionStrategy.WRAP) + assert _types_are_compatible(sender=Literal["a"], receiver=list[str]) == (True, ConversionStrategy.WRAP) + assert _types_are_compatible(sender=Literal["a", "b"], receiver=List[str]) == (True, ConversionStrategy.WRAP) + assert _types_are_compatible(sender=Literal["a"], receiver=list[int]) == (False, None) assert _types_are_compatible(sender=ChatMessage, receiver=List[str]) == ( True,