AttributeError when Using Numeric Variables in Agent Chat Moderation #6836

Closed
opened 2026-02-21 18:17:32 -05:00 by yindo · 1 comment
Owner

Originally created by @jingjingxinshang on GitHub (Nov 19, 2024).

Originally assigned to: @laipz8200 on GitHub.

Self Checks

  • 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 (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.11.2

Cloud or Self Hosted

Cloud, Self Hosted (Docker), Self Hosted (Source)

Steps to reproduce

When using content moderation and keyword filtering features in the Agent chat application, an error occurs if variables are set as numeric types. Specifically, this error is thrown by the backend API module:

Traceback (most recent call last):
  File "/app/api/core/app/apps/agent_chat/app_generator.py", line 213, in _generate_worker
    runner.run(
  File "/app/api/core/app/apps/agent_chat/app_runner.py", line 98, in run
    _, inputs, query = self.moderation_for_inputs(
  File "/app/api/core/app/apps/base_app_runner.py", line 349, in moderation_for_inputs
    return moderation_feature.check(
  File "/app/api/core/moderation/input_moderation.py", line 47, in check
    moderation_result = moderation_factory.moderation_for_inputs(inputs, query)
  File "/app/api/core/moderation/factory.py", line 37, in moderation_for_inputs
    return self.__extension_instance.moderation_for_inputs(inputs, query)
  File "/app/api/core/moderation/keywords/keywords.py", line 41, in moderation_for_inputs
    flagged = self._is_violated(inputs, keywords_list)
  File "/app/api/core/moderation/keywords/keywords.py", line 63, in _is_violated
    return any(self._check_keywords_in_value(keywords_list, value) for value in inputs.values())
  File "/app/api/core/moderation/keywords/keywords.py", line 63, in <genexpr>
    return any(self._check_keywords_in_value(keywords_list, value) for value in inputs.values())
  File "/app/api/core/moderation/keywords/keywords.py", line 66, in _check_keywords_in_value
    return any(keyword.lower() in value.lower() for keyword in keywords_list)
  File "/app/api/core/moderation/keywords/keywords.py", line 66, in <genexpr>
    return any(keyword.lower() in value.lower() for keyword in keywords_list)
AttributeError: 'int' object has no attribute 'lower'

Additionally, if the variable is set as optional and left empty, the following error occurs

AttributeError: 'NoneType' object has no attribute 'lower'

Steps to Reproduce:

1、Create an Agent Chat:

Start a new Agent chat session in the application.

2、Define a Numeric Variable:

In the chat setup, define a variable with a numeric type (e.g., age = 30).

3、Enable Content Moderation and Keyword Filtering Features:

Navigate to the settings or configuration section of the Agent.
Turn on content moderation and keyword filtering options.

4、Initiate a Chat:

Begin the chat session with a user (real or simulated).
Input a message that may cause the moderation feature to evaluate the defined numeric variable.

5、Observe the Output:

Monitor the backend API module for errors. If the system throws an error related to the numeric variable during content moderation, note the details.

✔️ Expected Behavior

It should work normally

Actual Behavior

An error occurs, and it cannot work

Originally created by @jingjingxinshang on GitHub (Nov 19, 2024). Originally assigned to: @laipz8200 on GitHub. ### Self Checks - [X] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.11.2 ### Cloud or Self Hosted Cloud, Self Hosted (Docker), Self Hosted (Source) ### Steps to reproduce When using content moderation and keyword filtering features in the Agent chat application, an error occurs if variables are set as numeric types. Specifically, this error is thrown by the backend API module: ``` Traceback (most recent call last): File "/app/api/core/app/apps/agent_chat/app_generator.py", line 213, in _generate_worker runner.run( File "/app/api/core/app/apps/agent_chat/app_runner.py", line 98, in run _, inputs, query = self.moderation_for_inputs( File "/app/api/core/app/apps/base_app_runner.py", line 349, in moderation_for_inputs return moderation_feature.check( File "/app/api/core/moderation/input_moderation.py", line 47, in check moderation_result = moderation_factory.moderation_for_inputs(inputs, query) File "/app/api/core/moderation/factory.py", line 37, in moderation_for_inputs return self.__extension_instance.moderation_for_inputs(inputs, query) File "/app/api/core/moderation/keywords/keywords.py", line 41, in moderation_for_inputs flagged = self._is_violated(inputs, keywords_list) File "/app/api/core/moderation/keywords/keywords.py", line 63, in _is_violated return any(self._check_keywords_in_value(keywords_list, value) for value in inputs.values()) File "/app/api/core/moderation/keywords/keywords.py", line 63, in <genexpr> return any(self._check_keywords_in_value(keywords_list, value) for value in inputs.values()) File "/app/api/core/moderation/keywords/keywords.py", line 66, in _check_keywords_in_value return any(keyword.lower() in value.lower() for keyword in keywords_list) File "/app/api/core/moderation/keywords/keywords.py", line 66, in <genexpr> return any(keyword.lower() in value.lower() for keyword in keywords_list) AttributeError: 'int' object has no attribute 'lower' ``` Additionally, if the variable is set as optional and left empty, the following error occurs AttributeError: 'NoneType' object has no attribute 'lower' Steps to Reproduce: 1、Create an Agent Chat: Start a new Agent chat session in the application. 2、Define a Numeric Variable: In the chat setup, define a variable with a numeric type (e.g., age = 30). 3、Enable Content Moderation and Keyword Filtering Features: Navigate to the settings or configuration section of the Agent. Turn on content moderation and keyword filtering options. 4、Initiate a Chat: Begin the chat session with a user (real or simulated). Input a message that may cause the moderation feature to evaluate the defined numeric variable. 5、Observe the Output: Monitor the backend API module for errors. If the system throws an error related to the numeric variable during content moderation, note the details. ### ✔️ Expected Behavior It should work normally ### ❌ Actual Behavior An error occurs, and it cannot work
yindo added the 🐞 bug label 2026-02-21 18:17:32 -05:00
yindo closed this issue 2026-02-21 18:17:32 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Nov 19, 2024):

@jingjingxinshang Hello there! 👋 I'm here to help you with any bugs, questions, or contributions you have in mind. Let's tackle this issue together!

The error you're encountering is due to the moderation feature attempting to call the lower() method on numeric and NoneType variables, which do not have this method. This is causing the AttributeError in the _check_keywords_in_value function when it tries to process these types of inputs.

To resolve this issue, the code should be updated to handle numeric and NoneType variables appropriately. Specifically, before calling lower(), the code should check if the variable is a string. If it's not, it should be converted to a string or handled in a way that avoids calling lower() on unsupported types.

Here's a potential solution:

  1. Convert Non-String Inputs: Modify the _check_keywords_in_value function to convert non-string inputs to strings before processing. This can be done by wrapping the value in str().

  2. Handle NoneType Separately: Add a check for NoneType variables and decide how they should be handled (e.g., skip them or treat them as empty strings).

Here's an example of how you might modify the function:

def _check_keywords_in_value(self, keywords_list, value) -> bool:
    if value is None:
        return False
    value_str = str(value)
    return any(keyword.lower() in value_str.lower() for keyword in keywords_list)

This change ensures that all inputs are converted to strings, preventing the AttributeError when lower() is called. Additionally, it handles NoneType by returning False, meaning no keywords are found in a None value.

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Nov 19, 2024): <!-- Greeting --> @jingjingxinshang Hello there! 👋 I'm here to help you with any bugs, questions, or contributions you have in mind. Let's tackle this issue together! <!-- Answer --> The error you're encountering is due to the moderation feature attempting to call the `lower()` method on numeric and `NoneType` variables, which do not have this method. This is causing the `AttributeError` in the `_check_keywords_in_value` function when it tries to process these types of inputs. To resolve this issue, the code should be updated to handle numeric and `NoneType` variables appropriately. Specifically, before calling `lower()`, the code should check if the variable is a string. If it's not, it should be converted to a string or handled in a way that avoids calling `lower()` on unsupported types. Here's a potential solution: 1. **Convert Non-String Inputs**: Modify the `_check_keywords_in_value` function to convert non-string inputs to strings before processing. This can be done by wrapping the `value` in `str()`. 2. **Handle `NoneType` Separately**: Add a check for `NoneType` variables and decide how they should be handled (e.g., skip them or treat them as empty strings). Here's an example of how you might modify the function: ```python def _check_keywords_in_value(self, keywords_list, value) -> bool: if value is None: return False value_str = str(value) return any(keyword.lower() in value_str.lower() for keyword in keywords_list) ``` This change ensures that all inputs are converted to strings, preventing the `AttributeError` when `lower()` is called. Additionally, it handles `NoneType` by returning `False`, meaning no keywords are found in a `None` value. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#6836