Skip to content

fix(rag): treat null and blank metadata values as empty in metadata filters - #43346

Open
msdianprince-7 wants to merge 1 commit into
langgenius:mainfrom
msdianprince-7:fix/metadata-empty-filter-json-null
Open

msdianprince-7 wants to merge 1 commit into
langgenius:mainfrom
msdianprince-7:fix/metadata-empty-filter-json-null

Conversation

@msdianprince-7

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 #43345

The metadata filter conditions empty / not empty checked doc_metadata -> '<field>' IS NULL. When a field is attached to a document without a value, the UI stores it as JSON null, and on PostgreSQL -> returns the JSONB value null, not SQL NULL. So those documents were skipped by empty and matched by not empty.

This change compares the extracted text value (as_string(), already used by the other conditions) instead. SQLAlchemy maps a JSON null to SQL NULL there on both PostgreSQL (->>) and MySQL. A blank string "" now also counts as empty.

Added a SQLite-backed test that stores documents with the key missing, null, "" and a real value, and checks which ones each condition returns. It fails on main and passes with this change.

Screenshots

Before After
empty misses documents whose value is null empty matches missing, null and "" values

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

🤖 Generated with Claude Code

From Claude Code

…ilters

The "empty" / "not empty" conditions checked the JSON element for SQL NULL,
so a field attached to a document without a value (stored as JSON null)
was treated as not empty. Compare the extracted text value instead, which
maps JSON null to NULL on PostgreSQL and MySQL, and count "" as empty.

Fixes langgenius#43345

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Oct 2, 2026
@ghfind-review ghfind-review Bot added the review: medium ghfind author score; see https://ghfind.com label Oct 2, 2026
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-10-02 06:53:41.395329849 +0000
+++ /tmp/pyrefly_pr.txt	2026-10-02 06:53:33.171171874 +0000
@@ -4251,7 +4251,7 @@
 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:5594:13
 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_methods.py:145:33
+   --> tests/unit_tests/core/rag/retrieval/test_dataset_retrieval_methods.py:147:33
 ERROR Argument `Iterator[SimpleNamespace]` is not assignable to parameter `invoke_result` with type `Generator[Unknown]` in function `core.rag.retrieval.router.multi_dataset_react_route.ReactMultiDatasetRouter._handle_invoke_result` [bad-argument-type]
    --> tests/unit_tests/core/rag/retrieval/test_multi_dataset_react_route.py:194:52
 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]

@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 69.73% 69.73% -0.00%
Strict coverage 69.38% 69.38% -0.00%
Typed symbols 55,329 55,333 +4
Untyped symbols 24,139 24,141 +2
Modules 3703 3703 0

@codecov

codecov Bot commented Oct 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.59%. Comparing base (ca27376) to head (7ae3616).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #43346      +/-   ##
==========================================
- Coverage   88.59%   88.59%   -0.01%     
==========================================
  Files        5182     5182              
  Lines      326050   326050              
  Branches    65397    65397              
==========================================
- Hits       288874   288866       -8     
- Misses      31928    31937       +9     
+ Partials     5248     5247       -1     
Flag Coverage Δ
api 88.36% <100.00%> (-0.01%) ⬇️

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.

@msdianprince-7

Copy link
Copy Markdown
Contributor Author

@JohnJyong friendly ping on this one, it still merges cleanly with main and CI is green. Happy to adjust anything if needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review: medium ghfind author score; see https://ghfind.com 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 "empty" / "not empty" ignores fields stored with a null value

1 participant