Skip to content

fix: raise FilterError on unknown metadata filter operators - #11795

Merged
davidsbatista merged 3 commits into
deepset-ai:mainfrom
immuhammadfurqan:fix/filter-unknown-operator-keyerror
Jun 29, 2026
Merged

fix: raise FilterError on unknown metadata filter operators#11795
davidsbatista merged 3 commits into
deepset-ai:mainfrom
immuhammadfurqan:fix/filter-unknown-operator-keyerror

Conversation

@immuhammadfurqan

@immuhammadfurqan immuhammadfurqan commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Related Issues

Fixes #11794

Proposed Changes

Metadata filters that use an unsupported operator previously raised a bare KeyError with no context. Because haystack/utils/filters.py is the shared filter engine used by InMemoryDocumentStore and every external document store, this cryptic error surfaced across all stores.

This PR validates the operator in both _comparison_condition and _logic_condition and raises a FilterError listing the valid operators — consistent with how other malformed-filter cases (missing keys, wrong value types) are already handled.

Before

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

After

FilterError: Unknown comparison operator 'gt'. Valid operators are: ['!=', '<', '<=', '==', '>', '>=', 'in', 'not in']

How did you test it?

Added two cases to the existing document_matches_filter_raises_error parametrization (unknown comparison operator and unknown logical operator). All filter unit tests pass; mypy is clean.

Checklist

@immuhammadfurqan
immuhammadfurqan requested a review from a team as a code owner June 27, 2026 21:01
@immuhammadfurqan
immuhammadfurqan requested review from bogdankostic and removed request for a team June 27, 2026 21:01
@vercel

vercel Bot commented Jun 27, 2026

Copy link
Copy Markdown

@immuhammadfurqan is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

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.
@immuhammadfurqan
immuhammadfurqan force-pushed the fix/filter-unknown-operator-keyerror branch from edcadec to 7bc8312 Compare June 27, 2026 21:04
@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/utils
  filters.py
Project Total  

This report was generated by python-coverage-comment-action

@vercel

vercel Bot commented Jun 29, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
haystack-docs Ready Ready Preview, Comment Jun 29, 2026 9:19am

Request Review

@davidsbatista davidsbatista left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution @immuhammadfurqan .
I extended the tests a bit.

@davidsbatista
davidsbatista merged commit 1a68ffa into deepset-ai:main Jun 29, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants