Skip to content

Commit 5bca520

Browse files
sjrldavidsbatista
andauthored
fix: Fix MetaFieldGroupingRanker to handle unhashable subgroup_by values like list (#9791)
* Fixes * Add reno --------- Co-authored-by: David S. Batista <dsbatista@gmail.com>
1 parent 10e05b6 commit 5bca520

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

haystack/components/rankers/meta_field_grouping_ranker.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,17 @@ def run(self, documents: list[Document]) -> dict[str, Any]:
9494
for doc in documents:
9595
group_value = str(doc.meta.get(self.group_by, ""))
9696

97-
if group_value:
98-
subgroup_value = "no_subgroup"
99-
if self.subgroup_by and self.subgroup_by in doc.meta:
100-
subgroup_value = doc.meta[self.subgroup_by]
101-
102-
document_groups[group_value][subgroup_value].append(doc)
103-
else:
97+
# If no group value, add to no_group_docs and continue
98+
if not group_value:
10499
no_group_docs.append(doc)
100+
continue
101+
102+
# Get subgroup value or use a default if not specified
103+
subgroup_value = "no_subgroup"
104+
if self.subgroup_by and self.subgroup_by in doc.meta:
105+
subgroup_value = str(doc.meta[self.subgroup_by])
106+
107+
document_groups[group_value][subgroup_value].append(doc)
105108

106109
ordered_docs = []
107110
for subgroups in document_groups.values():
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
Fix `MetaFieldGroupingRanker` to still work when `subgroup_by` values are unhashable types like list. We handle this by stringfying the contents of `doc.meta[subgroup_by]` in the same we do this for values of `doc.meta[group_by]`.

test/components/rankers/test_meta_field_grouping_ranker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def test_run_with_lists(self) -> None:
9797
"""
9898
Test if the MetaFieldGroupingRanker component can handle list values in the metadata.
9999
"""
100-
ranker = MetaFieldGroupingRanker(group_by="value_list", subgroup_by="subvaluelist", sort_docs_by="split_id")
100+
ranker = MetaFieldGroupingRanker(group_by="value_list", subgroup_by="sub_value_list", sort_docs_by="split_id")
101101
result = ranker.run(documents=DOC_LIST)
102102
assert "documents" in result
103103
assert len(DOC_LIST) == len(result["documents"])

0 commit comments

Comments
 (0)