Skip to content

Metadata filter "empty" / "not empty" ignores fields stored with a null value #43345

Open

Description

@msdianprince-7

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • 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

  1. In a knowledge base, create a metadata field author (string).
  2. 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}.
  3. In a Knowledge Retrieval node (or chat app), add a manual metadata filter author empty, 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 null
from (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.

Activity

  1. added
    review: medium
    on Oct 2, 2026
  2. dosubot commented on Oct 2, 2026

    @dosubot

    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]:

    case "empty":
        filters.append(DatasetDocument.doc_metadata[metadata_name].is_(None))
    
    case "not empty":
        filters.append(DatasetDocument.doc_metadata[metadata_name].isnot(None))

    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.

    To reply, just mention @dosu.


    Docs are dead. Just use Dosu.

    Leave Feedback Ask Dosu about dify

  3. added a commit that references this issue on Oct 2, 2026
    7ae3616
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    review: medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions