Skip to content

Commit c0868f3

Browse files
sjrlanakin87
authored andcommitted
fix: Make output_type optional in MetadataRouter.from_dict for YAML loading (#9724)
* Make output type optional in yaml * Add reno
1 parent 417b895 commit c0868f3

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

haystack/components/routers/metadata_router.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,7 @@ def from_dict(cls, data: dict[str, Any]) -> "MetadataRouter":
157157
The deserialized component instance.
158158
"""
159159
init_params = data.get("init_parameters", {})
160-
init_params["output_type"] = deserialize_type(init_params["output_type"])
160+
if "output_type" in init_params:
161+
# Deserialize the output_type to its original type
162+
init_params["output_type"] = deserialize_type(init_params["output_type"])
161163
return default_from_dict(cls, data)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed the `from_dict` method of `MetadataRouter` so the `output_type` parameter introduced in Haystack 2.17 is now optional when loading from YAML. This ensures compatibility with older Haystack pipelines.

test/components/routers/test_metadata_router.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,27 @@ def test_from_dict_with_parameters(self):
174174
}
175175
assert router.output_type == list[Union[ByteStream, Document]]
176176

177+
def test_from_dict_no_output_type(self):
178+
router_dict = {
179+
"type": "haystack.components.routers.metadata_router.MetadataRouter",
180+
"init_parameters": {
181+
"rules": {
182+
"edge_1": {
183+
"operator": "AND",
184+
"conditions": [{"field": "meta.created_at", "operator": ">=", "value": "2025-02-01"}],
185+
}
186+
}
187+
},
188+
}
189+
router = MetadataRouter.from_dict(router_dict)
190+
assert router.rules == {
191+
"edge_1": {
192+
"operator": "AND",
193+
"conditions": [{"field": "meta.created_at", "operator": ">=", "value": "2025-02-01"}],
194+
}
195+
}
196+
assert router.output_type == list[Document]
197+
177198
def test_metadata_router_in_pipeline(self):
178199
document_store = InMemoryDocumentStore()
179200
p = Pipeline()

0 commit comments

Comments
 (0)