Skip to content

Commit

Permalink
fix: OutputAdapter from_dict with custom_filters None (#8173)
Browse files Browse the repository at this point in the history
Co-authored-by: Marie-Luise Klaus <marieluise.klaus@deepset.ai>
  • Loading branch information
faymarie and Marie-Luise Klaus authored Aug 8, 2024
1 parent a4eb88e commit ec02817
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
9 changes: 7 additions & 2 deletions haystack/components/converters/output_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,13 @@ def from_dict(cls, data: Dict[str, Any]) -> "OutputAdapter":
"""
init_params = data.get("init_parameters", {})
init_params["output_type"] = deserialize_type(init_params["output_type"])
for name, filter_func in init_params.get("custom_filters", {}).items():
init_params["custom_filters"][name] = deserialize_callable(filter_func) if filter_func else None

custom_filters = init_params.get("custom_filters", {})
if custom_filters:
init_params["custom_filters"] = {
name: deserialize_callable(filter_func) if filter_func else None
for name, filter_func in custom_filters.items()
}
return default_from_dict(cls, data)

def _extract_variables(self, env: SandboxedEnvironment) -> Set[str]:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Fix Output Adapter from_dict method when custom_filters value is None.
16 changes: 16 additions & 0 deletions test/components/converters/test_output_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ def test_sede_with_multiple_custom_filters(self):
# invoke the custom filter to check if it is deserialized correctly
assert deserialized_adapter.custom_filters["custom_filter"]("test") == "TEST"

def test_output_adapter_from_dict_custom_filters_none(self):
component = OutputAdapter.from_dict(
data={
"type": "haystack.components.converters.output_adapter.OutputAdapter",
"init_parameters": {
"template": "{{ documents[0].content}}",
"output_type": "str",
"custom_filters": None,
},
}
)

assert component.template == "{{ documents[0].content}}"
assert component.output_type == str
assert component.custom_filters == {}

def test_output_adapter_in_pipeline(self):
@component
class DocumentProducer:
Expand Down

0 comments on commit ec02817

Please sign in to comment.