[PR #28155] fix: fix numeric type conversion issue in if-else condition comparison #31964

Closed
opened 2026-02-21 20:50:29 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/28155

State: closed
Merged: Yes


Summary

fix #28154

This PR fixes a numeric type conversion issue in if-else condition comparison functions. When comparing integer values with float strings (e.g., 0 <= "0.95"), the code was incorrectly converting float strings to integers, causing precision loss and incorrect evaluation results.

Changes

  • Add _normalize_numeric_values function to handle numeric type normalization uniformly
  • Refactor numeric comparison functions (greater than, less than, greater than or equal, less than or equal) to use unified type conversion logic
  • Fix comparison issues between integers, floats, and string numeric values
  • Add unit tests to verify numeric formatting and comparison functionality

Root Cause

The comparison functions (_assert_greater_than, _assert_less_than, _assert_greater_than_or_equal, _assert_less_than_or_equal) in core/workflow/utils/condition/processor.py had inconsistent type conversion logic. When comparing int values with float strings, the code would convert the string to int, losing precision (e.g., "0.95" becomes 0).

Solution

Introduced a unified _normalize_numeric_values function that:

  • Converts string numeric values to appropriate numeric types (int or float)
  • Preserves precision when comparing integers with float strings
  • Handles edge cases properly (e.g., whole number floats converted to ints when comparing with ints)

Testing

Added comprehensive unit tests in tests/unit_tests/core/workflow/utils/test_condition.py covering:

  • Integer vs float string comparisons (0 <= "0.95", 1 >= "0.95")
  • Float vs float string comparisons (1.1 >= "0.95")
  • Float vs integer string comparisons (1.1 > "0")

Screenshots

Before After
0 <= "0.95" evaluates to False (incorrect) 0 <= "0.95" evaluates to True (correct)
1 >= "0.95" evaluates to False (incorrect) 1 >= "0.95" evaluates to True (correct)

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 added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/28155 **State:** closed **Merged:** Yes --- ## Summary fix #28154 This PR fixes a numeric type conversion issue in if-else condition comparison functions. When comparing integer values with float strings (e.g., `0 <= "0.95"`), the code was incorrectly converting float strings to integers, causing precision loss and incorrect evaluation results. ### Changes - Add `_normalize_numeric_values` function to handle numeric type normalization uniformly - Refactor numeric comparison functions (greater than, less than, greater than or equal, less than or equal) to use unified type conversion logic - Fix comparison issues between integers, floats, and string numeric values - Add unit tests to verify numeric formatting and comparison functionality ### Root Cause The comparison functions (`_assert_greater_than`, `_assert_less_than`, `_assert_greater_than_or_equal`, `_assert_less_than_or_equal`) in `core/workflow/utils/condition/processor.py` had inconsistent type conversion logic. When comparing `int` values with float strings, the code would convert the string to `int`, losing precision (e.g., `"0.95"` becomes `0`). ### Solution Introduced a unified `_normalize_numeric_values` function that: - Converts string numeric values to appropriate numeric types (int or float) - Preserves precision when comparing integers with float strings - Handles edge cases properly (e.g., whole number floats converted to ints when comparing with ints) ### Testing Added comprehensive unit tests in `tests/unit_tests/core/workflow/utils/test_condition.py` covering: - Integer vs float string comparisons (`0 <= "0.95"`, `1 >= "0.95"`) - Float vs float string comparisons (`1.1 >= "0.95"`) - Float vs integer string comparisons (`1.1 > "0"`) ## Screenshots | Before | After | |--------|-------| | `0 <= "0.95"` evaluates to `False` (incorrect) | `0 <= "0.95"` evaluates to `True` (correct) | | `1 >= "0.95"` evaluates to `False` (incorrect) | `1 >= "0.95"` evaluates to `True` (correct) | ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:50:29 -05:00
yindo closed this issue 2026-02-21 20:50:29 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#31964