Metadata filters on number fields match nothing when the value comes from an app variable
Self Checks
- [x] I have read the Contributing Guide and Language Policy.
- [x] This is only for bug report, if you would like to ask a question, please head to Discussions.
- [x] I have searched for existing issues search for existing issues, including closed ones.
- [x] I confirm that I am using English to submit this report, otherwise it will be closed.
- [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
- [x] Please do not modify this template :) and fill in all the required fields.
Dify version
main (since #44483, not in a release yet)
Cloud or Self Hosted
Self Hosted (Source)
Steps to reproduce
- Create a knowledge base with a
numbermetadata fieldyear. Set it to2023on one document and2025on another. - In a Chat or Agent app that uses this knowledge base, add a number variable
yearand a manual metadata filteryear = {{year}}. - Chat with
yearset to2025.
The app replaces {{year}} with the string "2025", so the filter compares a string with a stored number.
✔️ Expected Behavior
year = 2025 finds the 2025 document, and year ≠ 2025 finds the 2023 document, as in 1.18.0.
❌ Actual Behavior
Neither filter finds a document, and there is no error:
| Filter value sent as a string | 1.18.0 | main |
|---|---|---|
year = "2025" |
2025 document | none |
year ≠ "2025" |
2023 document | none |
The 1.18.0 results come from the Service API retrieve endpoint. The main results come from running the SQL that main generates on the same Postgres data, not from a running app.
In process_metadata_filter_func, a string value now only matches metadata stored as a JSON string. Before #44483, it was compared with the stored value as text, so it also matched numbers.
If a fixed year works for your app, switch the manual filter from Variable to Constant and enter 2025. The editor sends the constant as a number, so it takes the numeric comparison path for both = and ≠. This doesn’t require changing any document metadata [1] [2] [3] [4].
The app code confirms why the variable behaves differently: it converts inputs to strings before the filter substitutes {{year}}, and both SQL operators then require a stored JSON string. #47826 is open for the dynamic case as of Dec 4. Since the PR reports SQLite tests and generated SQL results on Postgres, the remaining check is to run both operators through a Chat or Agent app with the variable selected [5] [6] [4] [7].
To reply, just mention @dosu.
Docs are dead. Just use Dosu.