Skip to content

Conversation

@qiqizjl
Copy link
Contributor

@qiqizjl qiqizjl commented Nov 13, 2025

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

- 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
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 13, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @qiqizjl, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses and resolves a critical bug (issue #28154) in the system's conditional processing logic, specifically concerning numeric type conversion during if-else condition comparisons. Previously, comparing integer values with float strings (e.g., 0 <= "0.95") would lead to incorrect evaluations due to precision loss when float strings were erroneously converted to integers. The solution introduces a standardized numeric normalization function, _normalize_numeric_values, which ensures accurate and consistent type handling across all numeric comparison operations, thereby preventing data loss and ensuring correct conditional logic execution.

Highlights

  • Unified Numeric Type Normalization: A new function, _normalize_numeric_values, has been introduced to consistently handle numeric type conversions for comparison operations, ensuring precision is maintained when comparing different numeric types, including string representations of numbers.
  • Refactored Comparison Functions: The core numeric comparison functions (_assert_greater_than, _assert_less_than, _assert_greater_than_or_equal, _assert_less_than_or_equal) have been refactored to leverage the new unified type conversion logic, removing inconsistent and error-prone type handling.
  • Fixed Numeric Comparison Issues: This PR resolves issues where comparisons between integers, floats, and string numeric values (e.g., 0 <= "0.95") would yield incorrect results due to precision loss during type conversion, specifically when float strings were incorrectly converted to integers.
  • Comprehensive Unit Tests: New unit tests have been added to thoroughly verify the numeric formatting and comparison functionality, covering various scenarios like integer vs. float string, float vs. float string, and float vs. integer string comparisons.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively addresses a numeric type conversion issue during condition comparisons by introducing a unified _normalize_numeric_values function. The refactoring of the numeric comparison functions to use this new utility is clean and centralizes the logic, which is a great improvement. The added unit tests correctly verify the fix for comparisons between integers, floats, and numeric strings.

My review includes a suggestion to further improve the consistency of type handling in the new normalization function. I've also added a few comments in the test file to align the code with Python's standard style guidelines (PEP 8), which will enhance readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Numeric type conversion issue in if-else condition comparison

1 participant