Skip to content

Commit edcadec

Browse files
fix: raise FilterError on unknown metadata filter operators
Filtering with an unsupported operator (e.g. a typo like "gt" instead of ">", or "XOR" as a logical operator) raised a bare KeyError with no context. Since filters.py is the shared filter engine used by InMemoryDocumentStore and every external document store, this affected all stores. Both _comparison_condition and _logic_condition now validate the operator and raise a FilterError that lists the valid operators, consistent with how other malformed-filter cases are already handled. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 0500a97 commit edcadec

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

haystack/utils/filters.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ def _logic_condition(condition: dict[str, Any], document: Document | ByteStream)
173173
msg = f"'conditions' key missing in {condition}"
174174
raise FilterError(msg)
175175
operator: str = condition["operator"]
176+
if operator not in LOGICAL_OPERATORS:
177+
msg = f"Unknown logical operator '{operator}'. Valid operators are: {sorted(LOGICAL_OPERATORS)}"
178+
raise FilterError(msg)
176179
conditions: list[dict[str, Any]] = condition["conditions"]
177180
return LOGICAL_OPERATORS[operator](document=document, conditions=conditions)
178181

@@ -214,5 +217,8 @@ def _comparison_condition(condition: dict[str, Any], document: Document | ByteSt
214217
else:
215218
document_value = getattr(document, field)
216219
operator: str = condition["operator"]
220+
if operator not in COMPARISON_OPERATORS:
221+
msg = f"Unknown comparison operator '{operator}'. Valid operators are: {sorted(COMPARISON_OPERATORS)}"
222+
raise FilterError(msg)
217223
filter_value: Any = condition["value"]
218224
return COMPARISON_OPERATORS[operator](filter_value=filter_value, value=document_value)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
fixes:
3+
- |
4+
Metadata filters that use an unsupported operator now raise a clear ``FilterError``
5+
that lists the valid operators, instead of a cryptic ``KeyError``. For example, a
6+
typo such as ``{"field": "meta.year", "operator": "gt", "value": 2000}`` (instead of
7+
``">"``) previously surfaced as ``KeyError: 'gt'``. This applies to both comparison
8+
operators and logical operators (``AND``, ``OR``, ``NOT``).

test/utils/test_filters.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,12 @@ def test_document_matches_filter(filters, document, expected_result):
566566
pytest.param({"operator": "==", "value": "test"}, id="Missing condition field key"),
567567
pytest.param({"field": "meta.name", "value": "test"}, id="Missing condition operator key"),
568568
pytest.param({"field": "meta.name", "operator": "=="}, id="Missing condition value key"),
569+
# Unknown operators
570+
pytest.param({"field": "meta.page", "operator": "gt", "value": 10}, id="Unknown comparison operator"),
571+
pytest.param(
572+
{"operator": "XOR", "conditions": [{"field": "meta.page", "operator": "==", "value": 10}]},
573+
id="Unknown logical operator",
574+
),
569575
]
570576

571577

0 commit comments

Comments
 (0)