Skip to content

Metadata filters raise a cryptic KeyError on unknown/typo'd operators instead of FilterError #11794

Description

@immuhammadfurqan

Describe the bug

When a metadata filter uses an operator that isn't supported (a typo such as gt/lt/eq/contains instead of >/</==/in, or a wrong logical operator like XOR), document_matches_filter raises a bare KeyError with no guidance, instead of the FilterError that the rest of the filter validation already uses.

This happens in haystack/utils/filters.py, which is the shared filter engine used by InMemoryDocumentStore and every external document store (Weaviate, Pinecone, OpenSearch, …), so the cryptic error surfaces across all stores.

Error message

KeyError: 'gt'

To Reproduce

from haystack.dataclasses import Document
from haystack.utils.filters import document_matches_filter

doc = Document(content="x", meta={"year": 2020})

# comparison operator typo
document_matches_filter({"field": "meta.year", "operator": "gt", "value": 2000}, doc)
# -> KeyError: 'gt'

# logical operator typo
document_matches_filter(
    {"operator": "XOR", "conditions": [{"field": "meta.year", "operator": "==", "value": 2020}]},
    doc,
)
# -> KeyError: 'XOR'

Expected behavior

A FilterError explaining that the operator is unsupported and listing the valid operators — consistent with how other malformed filters (missing keys, wrong value types) are already reported.

System:

  • Haystack version: main

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium priority, add to the next sprint if no P1 available

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions