Cannot load custom Moderation extension #2794

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

Originally created by @lo5twind on GitHub (Apr 29, 2024).

Self Checks

  • This is only for bug report, if you would like to ask a quesion, 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).
  • Pleas do not modify this template :) and fill in all the required fields.

Dify version

0.6.5

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

When I wrote a custom Moderation extension according to the official documentation, a loading error occurred during the restart of the service. The error message is as follows:

Traceback (most recent call last):
  File "/usr/local/bin/flask", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.10/site-packages/flask/cli.py", line 1105, in main
    cli.main()
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1682, in invoke
    cmd_name, cmd, args = self.resolve_command(ctx, args)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1729, in resolve_command
    cmd = self.get_command(ctx, cmd_name)
  File "/usr/local/lib/python3.10/site-packages/flask/cli.py", line 611, in get_command
    app = info.load_app()
  File "/usr/local/lib/python3.10/site-packages/flask/cli.py", line 335, in load_app
    app = locate_app(import_name, name)
  File "/usr/local/lib/python3.10/site-packages/flask/cli.py", line 245, in locate_app
    __import__(module_name)
  File "/app/api/app.py", line 210, in <module>
    app = create_app()
  File "/app/api/app.py", line 105, in create_app
    initialize_extensions(app)
  File "/app/api/app.py", line 116, in initialize_extensions
    ext_code_based_extension.init()
  File "/app/api/extensions/ext_code_based_extension.py", line 5, in init
    code_based_extension.init()
  File "/app/api/core/extension/extension.py", line 16, in init
    self.__module_extensions[module.value] = module_class.scan_extensions()
  File "/app/api/core/extension/extensible.py", line 111, in scan_extensions
    sorted_extensions = sort_to_dict_by_position_map(position_map, extensions, lambda x: x.name)
  File "/app/api/core/utils/position_helper.py", line 69, in sort_to_dict_by_position_map
    sorted_items = sort_by_position_map(position_map, data, name_func)
  File "/app/api/core/utils/position_helper.py", line 53, in sort_by_position_map
    return sorted(data, key=lambda x: position_map.get(name_func(x), float('inf')))
TypeError: '<' not supported between instances of 'NoneType' and 'int'

When I check the source code, I found that a non-builtin extension will set a None in position_map that will make this TypeError, please help to fix that.

I try to modify the code at api/core/extension/extensible.py line 68, like below:

                if '__builtin__' in file_names:
                    builtin = True

                    builtin_file_path = os.path.join(subdir_path, '__builtin__')
                    if os.path.exists(builtin_file_path):
                        with open(builtin_file_path, encoding='utf-8') as f:
                            position = int(f.read().strip())
                    position_map[extension_name] = position                  # modified work
                #position_map[extension_name] = position                   # original  NOT work

✔️ Expected Behavior

load extension success

Actual Behavior

TypeError exception was thrown.

Originally created by @lo5twind on GitHub (Apr 29, 2024). ### Self Checks - [X] This is only for bug report, if you would like to ask a quesion, 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] Pleas do not modify this template :) and fill in all the required fields. ### Dify version 0.6.5 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce When I wrote a custom Moderation extension according to the official documentation, a loading error occurred during the restart of the service. The error message is as follows: ``` Traceback (most recent call last): File "/usr/local/bin/flask", line 8, in <module> sys.exit(main()) File "/usr/local/lib/python3.10/site-packages/flask/cli.py", line 1105, in main cli.main() File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1078, in main rv = self.invoke(ctx) File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1682, in invoke cmd_name, cmd, args = self.resolve_command(ctx, args) File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1729, in resolve_command cmd = self.get_command(ctx, cmd_name) File "/usr/local/lib/python3.10/site-packages/flask/cli.py", line 611, in get_command app = info.load_app() File "/usr/local/lib/python3.10/site-packages/flask/cli.py", line 335, in load_app app = locate_app(import_name, name) File "/usr/local/lib/python3.10/site-packages/flask/cli.py", line 245, in locate_app __import__(module_name) File "/app/api/app.py", line 210, in <module> app = create_app() File "/app/api/app.py", line 105, in create_app initialize_extensions(app) File "/app/api/app.py", line 116, in initialize_extensions ext_code_based_extension.init() File "/app/api/extensions/ext_code_based_extension.py", line 5, in init code_based_extension.init() File "/app/api/core/extension/extension.py", line 16, in init self.__module_extensions[module.value] = module_class.scan_extensions() File "/app/api/core/extension/extensible.py", line 111, in scan_extensions sorted_extensions = sort_to_dict_by_position_map(position_map, extensions, lambda x: x.name) File "/app/api/core/utils/position_helper.py", line 69, in sort_to_dict_by_position_map sorted_items = sort_by_position_map(position_map, data, name_func) File "/app/api/core/utils/position_helper.py", line 53, in sort_by_position_map return sorted(data, key=lambda x: position_map.get(name_func(x), float('inf'))) TypeError: '<' not supported between instances of 'NoneType' and 'int' ``` When I check the source code, I found that a non-builtin extension will set a `None` in `position_map` that will make this TypeError, please help to fix that. I try to modify the code at api/core/extension/extensible.py line 68, like below: ```python if '__builtin__' in file_names: builtin = True builtin_file_path = os.path.join(subdir_path, '__builtin__') if os.path.exists(builtin_file_path): with open(builtin_file_path, encoding='utf-8') as f: position = int(f.read().strip()) position_map[extension_name] = position # modified work #position_map[extension_name] = position # original NOT work ``` ### ✔️ Expected Behavior load extension success ### ❌ Actual Behavior TypeError exception was thrown.
yindo added the 🐞 bug label 2026-02-21 17:50:06 -05:00
yindo closed this issue 2026-02-21 17:50:06 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#2794