You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Metadata filter "empty" / "not empty" ignores fields stored with a null value #43345
I confirm that I am using English to submit this report, otherwise it will be closed.
【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
Please do not modify this template :) and fill in all the required fields.
Dify version
main (1.17.1)
Cloud or Self Hosted
Self Hosted (Source)
Steps to reproduce
In a knowledge base, create a metadata field author (string).
Attach author to document A without setting a value, and set author = "bob" on document B. The UI stores document A's metadata as {"author": null}.
In a Knowledge Retrieval node (or chat app), add a manual metadata filter authorempty, and run a query that matches both documents.
✔️ Expected Behavior
empty returns document A (and documents without the field); not empty returns only document B.
❌ Actual Behavior
empty does not return document A, and not empty returns both A and B.
DatasetRetrieval.process_metadata_filter_func builds doc_metadata -> 'author' IS NULL. On PostgreSQL -> returns the JSONB value null for a stored null, which is not SQL NULL:
select d->'author' is null, d->>'author' is nullfrom (values ('{}'::jsonb), ('{"author": null}'::jsonb)) t(d);
-- {} | t | t-- {"author": null} | f | t
So only documents where the key is missing count as empty. A blank string "" is also treated as not empty. #24275 reported the same symptom and was closed, but the code still uses the JSON element IS NULL check.
This reproduces on current main. In process_metadata_filter_func, every other operator builds json_field = DatasetDocument.doc_metadata[metadata_name].as_string() (the ->> text extraction), but the empty/not empty branches skip it and compare the raw JSON element [1][2]:
Without .as_string(), doc_metadata[metadata_name] is the -> operator. On Postgres/JSONB it returns the JSON element for a stored {"author": null}, not SQL NULL. As a result, IS NULL/IS NOT NULL only distinguish "key missing" from "key present," never "value is null" or "".
PR #24276, which closed #24275, only changed the earlier guard that short-circuited the whole function when value is None[3]. It changed if value is None: to if value is None and condition not in ("empty", "not empty"): so the empty/not empty branches would run at all. It didn't change how those branches build the filter, so the JSONB null vs. SQL null gap was never addressed.
Both the Knowledge Retrieval node and DatasetRetrieval call this same classmethod, so a single fix covers both. It would need to handle the stored-null, missing-key, and blank-string cases, likely by switching to the .as_string()-based json_field (e.g. json_field.is_(None) | (json_field == 'null') style handling) or by casting appropriately.
Activity
dosubot commentedon Oct 2, 2026
This reproduces on current
main. Inprocess_metadata_filter_func, every other operator buildsjson_field = DatasetDocument.doc_metadata[metadata_name].as_string()(the->>text extraction), but theempty/not emptybranches skip it and compare the raw JSON element [1] [2]:Without
.as_string(),doc_metadata[metadata_name]is the->operator. On Postgres/JSONB it returns the JSON element for a stored{"author": null}, not SQLNULL. As a result,IS NULL/IS NOT NULLonly distinguish "key missing" from "key present," never "value is null" or"".PR #24276, which closed #24275, only changed the earlier guard that short-circuited the whole function when
value is None[3]. It changedif value is None:toif value is None and condition not in ("empty", "not empty"):so theempty/not emptybranches would run at all. It didn't change how those branches build the filter, so the JSONB null vs. SQL null gap was never addressed.Both the Knowledge Retrieval node and
DatasetRetrievalcall this same classmethod, so a single fix covers both. It would need to handle the stored-null, missing-key, and blank-string cases, likely by switching to the.as_string()-basedjson_field(e.g.json_field.is_(None) | (json_field == 'null')style handling) or by casting appropriately.To reply, just mention @dosu.
Docs are dead. Just use Dosu.
fix(rag): treat null and blank metadata values as empty in metadata f…