|
3 | 3 | # SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | 5 | import asyncio |
| 6 | +from typing import Optional |
6 | 7 |
|
7 | 8 | import pytest |
8 | 9 |
|
9 | | -from haystack import AsyncPipeline |
| 10 | +from haystack import AsyncPipeline, component |
10 | 11 |
|
11 | 12 |
|
12 | 13 | def test_async_pipeline_reentrance(waiting_component, spying_tracer): |
@@ -43,3 +44,77 @@ async def call_run(): |
43 | 44 |
|
44 | 45 | with pytest.raises(RuntimeError, match="Cannot call run\\(\\) from within an async context"): |
45 | 46 | asyncio.run(call_run()) |
| 47 | + |
| 48 | + |
| 49 | +def test_component_with_empty_dict_as_output_appears_in_results(): |
| 50 | + """Test that components that return an empty dict as output appear in results as an empty dict""" |
| 51 | + |
| 52 | + @component |
| 53 | + class Producer: |
| 54 | + def __init__(self, prefix: str): |
| 55 | + self.prefix = prefix |
| 56 | + |
| 57 | + @component.output_types(value=Optional[str]) |
| 58 | + def run(self, text: Optional[str]): |
| 59 | + return {"value": f"{self.prefix}: {text}"} |
| 60 | + |
| 61 | + @component.output_types(value=Optional[str]) |
| 62 | + async def run_async(self, text: Optional[str]): |
| 63 | + return {"value": f"{self.prefix}: {text}"} |
| 64 | + |
| 65 | + @component |
| 66 | + class EmptyProcessor: |
| 67 | + @component.output_types() |
| 68 | + def run(self, sources: list[str]): |
| 69 | + # Returns empty dict when sources is empty |
| 70 | + return {} |
| 71 | + |
| 72 | + @component.output_types() |
| 73 | + async def run_async(self, sources: list[str]): |
| 74 | + # Returns empty dict when sources is empty |
| 75 | + return {} |
| 76 | + |
| 77 | + @component |
| 78 | + class Combiner: |
| 79 | + @component.output_types(combined=str) |
| 80 | + def run(self, input_a: Optional[str], input_b: Optional[str]): |
| 81 | + if input_a is None: |
| 82 | + input_a = "" |
| 83 | + if input_b is None: |
| 84 | + input_b = "" |
| 85 | + return {"combined": f"{input_a} | {input_b}"} |
| 86 | + |
| 87 | + @component.output_types(combined=str) |
| 88 | + async def run_async(self, input_a: Optional[str], input_b: Optional[str]): |
| 89 | + if input_a is None: |
| 90 | + input_a = "" |
| 91 | + if input_b is None: |
| 92 | + input_b = "" |
| 93 | + return {"combined": f"{input_a} | {input_b}"} |
| 94 | + |
| 95 | + pp = AsyncPipeline() |
| 96 | + pp.add_component("producer_a", Producer("A")) |
| 97 | + pp.add_component("producer_b", Producer("B")) |
| 98 | + pp.add_component("empty_processor", EmptyProcessor()) |
| 99 | + pp.add_component("combiner", Combiner()) |
| 100 | + |
| 101 | + pp.connect("producer_a.value", "combiner.input_a") |
| 102 | + pp.connect("producer_b.value", "combiner.input_b") |
| 103 | + |
| 104 | + result = pp.run( |
| 105 | + {"producer_a": {"text": "hello"}, "producer_b": {"text": "world"}, "empty_processor": {"sources": []}}, |
| 106 | + include_outputs_from={"producer_a", "empty_processor", "combiner"}, |
| 107 | + ) |
| 108 | + |
| 109 | + # Producer A should appear in results because it's in include_outputs_from |
| 110 | + assert "producer_a" in result |
| 111 | + assert result["producer_a"] == {"value": "A: hello"} |
| 112 | + # Producer B should NOT appear since it's not in include_outputs_from |
| 113 | + assert "producer_b" not in result |
| 114 | + # Combiner should appear in results |
| 115 | + assert "combiner" in result |
| 116 | + assert result["combiner"] == {"combined": "A: hello | B: world"} |
| 117 | + # Empty processor should appear in results even though it returns an empty dict |
| 118 | + # because it's in include_outputs_from |
| 119 | + assert "empty_processor" in result |
| 120 | + assert result["empty_processor"] == {} |
0 commit comments