From 151bd531a5b94eb644624402844879d243312951 Mon Sep 17 00:00:00 2001 From: Madeesh Kannan Date: Thu, 19 Sep 2024 15:03:04 +0200 Subject: [PATCH] feat: Add utility function to check metadata filter syntax (#8386) --- haystack/utils/filters.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/haystack/utils/filters.py b/haystack/utils/filters.py index 8cdf294ffb..a24affaf1b 100644 --- a/haystack/utils/filters.py +++ b/haystack/utils/filters.py @@ -4,7 +4,7 @@ from dataclasses import fields from datetime import datetime -from typing import Any, Dict, List +from typing import Any, Dict, List, Optional import pandas as pd @@ -12,6 +12,15 @@ from haystack.errors import FilterError +def raise_on_invalid_filter_syntax(filters: Optional[Dict[str, Any]] = None): + """ + Raise an error if the filter syntax is invalid. + """ + if filters and "operator" not in filters and "conditions" not in filters: + msg = "Invalid filter syntax. See https://docs.haystack.deepset.ai/docs/metadata-filtering for details." + raise ValueError(msg) + + def document_matches_filter(filters: Dict[str, Any], document: Document) -> bool: """ Return whether `filters` match the Document.