dify icon indicating copy to clipboard operation
dify copied to clipboard

A non-numeric value in a number or time metadata field makes metadata-filtered retrieval fail with 500 for the whole knowledge base

Open cty-ut opened this issue 1 month ago • 1 comments

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

1.18.0

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

  1. In a knowledge base with documents, create a metadata field year of type number, and give some documents numeric values (e.g. 2023, 2025).

  2. Retrieve with a numeric filter. It works (HTTP 200):

    curl -X POST "$BASE/v1/datasets/$DATASET_ID/retrieve" \
      -H "Authorization: Bearer $API_KEY" -H "Content-Type: application/json" \
      -d '{"query":"doc","retrieval_model":{"search_method":"keyword_search","top_k":5,
           "score_threshold_enabled":false,"reranking_enable":false,
           "metadata_filtering_conditions":{"logical_operator":"and",
             "conditions":[{"name":"year","comparison_operator":">","value":2024}]}}}'
    
  3. Set year to a non-numeric string on one document. The request is accepted (HTTP 200, {"result": "success"}):

    curl -X POST "$BASE/v1/datasets/$DATASET_ID/documents/metadata" \
      -H "Authorization: Bearer $API_KEY" -H "Content-Type: application/json" \
      -d '{"operation_data":[{"document_id":"'$DOCUMENT_ID'","partial_update":true,
           "metadata_list":[{"id":"'$YEAR_FIELD_ID'","name":"year","value":"abc"}]}]}'
    

✔️ Expected Behavior

Step 3 is rejected with 400, because abc is not a valid value for a number field. Retrieval keeps working.

❌ Actual Behavior

Step 3 succeeds, and from then on step 4, and every other retrieval of this knowledge base with a numeric filter, returns 500:

sqlalchemy.exc.DataError: (psycopg2.errors.InvalidTextRepresentation) invalid input syntax for type double precision: "abc"

SQLAlchemyMetadataRepository.update_documents (api/repositories/knowledge/metadata_repository.py) already loads each field's DatasetMetadata row, including its type, but never checks the value against it, so any value is stored. At query time, process_metadata_filter_func compares number and time fields with as_float(), i.e. a CAST(... AS FLOAT). One stored value that cannot be cast fails the whole query.

That function is shared by every retrieval path that applies metadata filters (chat apps, the workflow Knowledge Retrieval node, and the Service API /retrieve), so one bad document breaks numeric and time filtering for all of them. Date strings such as "2025-01-01" in a time field are an easy way to hit this, since time values are stored as Unix timestamps.

#27358 looks like the same symptom (number/time filters failing over the API). It was closed as stale without a root cause.

I'd like to work on this.

cty-ut avatar Nov 05 '26 08:11 cty-ut

The current checkout matches the write-side gap: update_documents resolves field IDs but stores each value without checking the field’s declared type. The request model also explicitly accepts strings and null. [1] [2]

For a contribution, I’d suggest regression coverage for:

  • Invalid number and time values returning 400 through the API, plus valid numbers, timestamps, and the existing nullable behavior. [3] [2]
  • A mixed valid/invalid batch leaving all documents unchanged, for both partial and full updates. The repository currently wraps the batch in one transaction; test_metadata_partial_update.py provides container-backed service tests to extend. [1] [4]

One scope point for maintainers: write validation alone would not repair already-stored invalid values. Please flag existing-data handling separately in the fix description, since retrieval would still reach the same .as_float() comparisons for those rows. [5] [6]

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about dify with Dosu

dosubot[bot] avatar Nov 05 '26 08:11 dosubot[bot]