Skip to content
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
10 changes: 10 additions & 0 deletions haystack/core/type_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 30 additions & 0 deletions test/core/pipeline/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import logging
from concurrent.futures import ThreadPoolExecutor
from typing import Literal

import pytest

Expand Down Expand Up @@ -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"]}}
12 changes: 11 additions & 1 deletion test/core/pipeline/test_pipeline_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]})()
Expand Down
16 changes: 16 additions & 0 deletions test/core/test_type_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"
),
Expand Down Expand Up @@ -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,
Expand Down
Loading