Skip to content

fix: include documents without the metadata key in negative metadata filters - #44753

Merged
asukaminato0721 merged 4 commits into
langgenius:mainfrom
cty-ut:fix/metadata-negative-filters-include-missing
Oct 26, 2026
Merged

asukaminato0721 merged 4 commits into
langgenius:mainfrom
cty-ut:fix/metadata-negative-filters-include-missing

Conversation

@cty-ut

@cty-ut cty-ut commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Fixes #44752

Metadata filters with negative operators currently exclude documents that don't have the metadata key, because NULL != 'Alice' evaluates to unknown and WHERE drops the row. This PR makes those operators include such documents. Each of the four negative operators now adds OR <field> IS NULL. Range operators (<, >, ≤, ≥, before, after) and positive operators are unchanged. A missing value can't be placed above or below a number, so it shouldn't match range operators; only negations hold for a missing value.

#43346 changes empty / not empty in the same function; this PR doesn't touch those branches.

Tests

Added a parametrized test backed by the unit-test SQLite database, using three documents:

document metadata
alice {"writer": "Alice", "year": 2023}
bob {"writer": "Bob", "year": 2025}
missing {}

The four negative-operator cases (is not, ≠, not contains, not in) fail before the fix and pass after it. The three control cases (is, in, >) pass both before and after, confirming positive and range operators are not affected.

Behavior change

This changes retrieval results for existing apps that use these negative operators: documents without the metadata key will now be returned. If maintainers consider the current behavior intentional, feel free to close this PR.

Screenshots

Before After
is not "Alice" → {bob} is not "Alice" → {bob, missing}
≠ 2023 → {bob} ≠ 2023 → {bob, missing}
not contains "Ali" → {bob} not contains "Ali" → {bob, missing}
not in "Alice" → {bob} not in "Alice" → {bob, missing}
is "Alice" → {alice} is "Alice" → {alice}
in "Alice" → {alice} in "Alice" → {alice}
> 2024 → {bob} > 2024 → {bob}

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've verified the change and added or updated tests where meaningful regression risk justifies coverage.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and vp staged (frontend) to appease the lint gods

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-10-26 03:04:29.603022635 +0000
+++ /tmp/pyrefly_pr.txt	2026-10-26 03:04:18.582986803 +0000
@@ -4247,71 +4247,71 @@
 ERROR Object of class `FunctionType` has no attribute `return_value` [missing-attribute]
     --> tests/unit_tests/core/rag/rerank/test_rerank_model.py:1847:9
 ERROR Unpacked keyword argument `object` is not assignable to parameter `id` with type `SQLCoreOperations[str] | str` in function `models.dataset.Dataset.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:117:20
+   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:118:20
 ERROR Unpacked keyword argument `object` is not assignable to parameter `tenant_id` with type `SQLCoreOperations[str] | str` in function `models.dataset.Dataset.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:117:20
+   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:118:20
 ERROR Unpacked keyword argument `object` is not assignable to parameter `name` with type `SQLCoreOperations[str] | str` in function `models.dataset.Dataset.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:117:20
+   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:118:20
 ERROR Unpacked keyword argument `object` is not assignable to parameter `description` with type `SQLCoreOperations[str | None] | str | None` in function `models.dataset.Dataset.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:117:20
+   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:118:20
 ERROR Unpacked keyword argument `object` is not assignable to parameter `provider` with type `SQLCoreOperations[str] | str` in function `models.dataset.Dataset.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:117:20
+   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:118:20
 ERROR Unpacked keyword argument `object` is not assignable to parameter `created_by` with type `SQLCoreOperations[str] | str` in function `models.dataset.Dataset.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:117:20
+   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:118:20
 ERROR Unpacked keyword argument `object` is not assignable to parameter `maintainer` with type `SQLCoreOperations[str | None] | str | None` in function `models.dataset.Dataset.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:117:20
+   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:118:20
 ERROR Unpacked keyword argument `object` is not assignable to parameter `updated_by` with type `SQLCoreOperations[str | None] | str | None` in function `models.dataset.Dataset.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:117:20
+   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:118:20
 ERROR Unpacked keyword argument `object` is not assignable to parameter `built_in_field_enabled` with type `SQLCoreOperations[bool] | bool` in function `models.dataset.Dataset.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:117:20
+   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:118:20
 ERROR Unpacked keyword argument `object` is not assignable to parameter `enable_api` with type `SQLCoreOperations[bool] | bool` in function `models.dataset.Dataset.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:117:20
+   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:118:20
 ERROR Unpacked keyword argument `object` is not assignable to parameter `is_multimodal` with type `SQLCoreOperations[bool] | bool` in function `models.dataset.Dataset.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:117:20
+   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:118:20
 ERROR Argument `_FakeFlaskApp` is not assignable to parameter `flask_app` with type `Flask` in function `core.rag.retrieval.dataset_retrieval.DatasetRetrieval._run_retriever_thread` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:3868:31
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:3869:31
 ERROR Argument `_FakeFlaskApp` is not assignable to parameter `flask_app` with type `Flask` in function `core.rag.retrieval.dataset_retrieval.DatasetRetrieval._run_retriever_thread_safely` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:3890:31
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:3891:31
 ERROR Argument `_FakeFlaskApp` is not assignable to parameter `flask_app` with type `Flask` in function `core.rag.retrieval.dataset_retrieval.DatasetRetrieval._run_retriever_thread_safely` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:3914:31
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:3915:31
 ERROR `SimpleNamespace` is not assignable to attribute `application_generate_entity` with type `EasyUIBasedAppGenerateEntity | None` [bad-assignment]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:4061:53
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:4097:53
 ERROR Argument `Literal['completed']` is not assignable to parameter `indexing_status` with type `IndexingStatus | SQLCoreOperations[IndexingStatus]` in function `models.dataset.Document.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:4355:37
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:4391:37
 ERROR Argument `Literal['completed']` is not assignable to parameter `indexing_status` with type `IndexingStatus | SQLCoreOperations[IndexingStatus]` in function `models.dataset.Document.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:4483:37
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:4519:37
 ERROR Object of class `FunctionType` has no attribute `return_value` [missing-attribute]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:4708:13
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:4744:13
 ERROR Argument `Literal['completed']` is not assignable to parameter `indexing_status` with type `IndexingStatus | SQLCoreOperations[IndexingStatus]` in function `models.dataset.Document.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:4843:33
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:4879:33
 ERROR Argument `Literal['completed']` is not assignable to parameter `status` with type `SQLCoreOperations[SegmentStatus] | SegmentStatus` in function `models.dataset.DocumentSegment.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:4859:24
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:4895:24
 ERROR Argument `Literal['completed']` is not assignable to parameter `indexing_status` with type `IndexingStatus | SQLCoreOperations[IndexingStatus]` in function `models.dataset.Document.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5426:37
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5462:37
 ERROR Argument `Literal['completed']` is not assignable to parameter `status` with type `SQLCoreOperations[SegmentStatus] | SegmentStatus` in function `models.dataset.DocumentSegment.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5456:28
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5492:28
 ERROR Argument `str | None` is not assignable to parameter `doc_id` with type `str` in function `_doc` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5497:28
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5533:28
 ERROR Argument `str | None` is not assignable to parameter `doc_id` with type `str` in function `_doc` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5508:28
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5544:28
 ERROR Argument `SimpleNamespace` is not assignable to parameter `orig` with type `BaseException` in function `sqlalchemy.exc.DBAPIError.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5547:17
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5583:17
 ERROR Argument `None` is not assignable to parameter `orig` with type `BaseException` in function `sqlalchemy.exc.DBAPIError.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5568:45
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5604:45
 ERROR Argument `SimpleNamespace` is not assignable to parameter `orig` with type `BaseException` in function `sqlalchemy.exc.DBAPIError.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5573:25
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5609:25
 ERROR Argument `SimpleNamespace` is not assignable to parameter `orig` with type `BaseException` in function `sqlalchemy.exc.DBAPIError.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5582:25
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5618:25
 ERROR Argument `SimpleNamespace` is not assignable to parameter `orig` with type `BaseException` in function `sqlalchemy.exc.DBAPIError.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5591:25
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5627:25
 ERROR Argument `SimpleNamespace` is not assignable to parameter `orig` with type `BaseException` in function `sqlalchemy.exc.DBAPIError.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5606:17
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5642:17
 ERROR Argument `SimpleNamespace` is not assignable to parameter `orig` with type `BaseException` in function `sqlalchemy.exc.DBAPIError.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5630:17
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5666:17
 ERROR Argument `Literal['completed']` is not assignable to parameter `indexing_status` with type `IndexingStatus | SQLCoreOperations[IndexingStatus]` in function `models.dataset.Document.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5783:41
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:5819:41
 ERROR Argument `str` is not assignable to parameter `metadata_filtering_mode` with type `Literal['automatic', 'disabled', 'manual']` in function `core.workflow.nodes.knowledge_retrieval.retrieval.KnowledgeRetrievalRequest.__init__` [bad-argument-type]
-    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:6090:33
+    --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py:6126:33
 ERROR Argument `None` is not assignable to parameter `text` with type `str` in function `core.rag.splitter.text_splitter.RecursiveCharacterTextSplitter.split_text` [bad-argument-type]
     --> tests/unit_tests/core/rag/splitter/test_text_splitter.py:1873:42
 ERROR Argument `list[FromClause]` is not assignable to parameter `tables` with type `Sequence[Table] | None` in function `sqlalchemy.sql.schema.MetaData.create_all` [bad-argument-type]

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 78.76% 78.76% +0.00%
Strict coverage 78.44% 78.44% +0.00%
Typed symbols 74,137 74,143 +6
Untyped symbols 20,073 20,073 0
Modules 3911 3911 0

@codecov

codecov Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.40%. Comparing base (082aa5b) to head (6db92d5).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #44753      +/-   ##
==========================================
- Coverage   89.40%   89.40%   -0.01%     
==========================================
  Files        5986     5986              
  Lines      375154   375153       -1     
  Branches    76074    76074              
==========================================
- Hits       335398   335397       -1     
- Misses      33718    33719       +1     
+ Partials     6038     6037       -1     
Flag Coverage Δ
api 89.19% <100.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@asukaminato0721
asukaminato0721 added this pull request to the merge queue Oct 26, 2026
@asukaminato0721 asukaminato0721 self-assigned this Oct 26, 2026
Merged via the queue into langgenius:main with commit 4caa56f Oct 26, 2026
47 checks passed
@laipz8200 laipz8200 mentioned this pull request Nov 3, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Metadata filter: negative operators (is not, ≠, not contains, not in) exclude documents that don't have the field — intended?

2 participants