Error when using numbers in startnode with OpenAI moderation
Self Checks
- [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 (我已阅读并同意 Language Policy).
- [X] Please do not modify this template :) and fill in all the required fields.
Dify version
0.6.8
Cloud or Self Hosted
Self Hosted (Docker), Self Hosted (Source)
Steps to reproduce
If you use the "number" input field for a start node and have the open moderation functionality enabled. you get error "TypeError: sequence item 3: expected str instance, int found"
✔️ Expected Behavior
Run the workflow
❌ Actual Behavior
Seems that the function:
def _is_violated(self, inputs: dict):
text = '\n'.join(inputs.values())
model_manager = ModelManager()
model_instance = model_manager.get_model_instance(
tenant_id=self.tenant_id,
provider="openai",
model_type=ModelType.MODERATION,
model="text-moderation-stable"
)
openai_moderation = model_instance.invoke_moderation(
text=text
)
return openai_moderation
in api/core/moderation/openai_moderation/openapi_moderation.py is throwing the error because it expects string when the number input is int.
The error you're encountering is due to the inputs dictionary containing non-string values, and the join method expects all items to be strings. To resolve this issue, you need to ensure that all values in the inputs dictionary are converted to strings before joining them.
Here's how you can modify the _is_violated function to handle this:
def _is_violated(self, inputs: dict):
# Convert all input values to strings
text = '\n'.join(str(value) for value in inputs.values())
model_manager = ModelManager()
model_instance = model_manager.get_model_instance(
tenant_id=self.tenant_id,
provider="openai",
model_type=ModelType.MODERATION,
model="text-moderation-stable"
)
openai_moderation = model_instance.invoke_moderation(
text=text
)
return openai_moderation
This modification ensures that all values in the inputs dictionary are converted to strings before they are joined, preventing the TypeError from occurring.
Details
Sources
- api/core/workflow/nodes/start/start_node.py
- api/core/model_manager.py
- en/features/workflow/node/question-classifier.md
- en/features/workflow/nodes/question-classifier.md
- web/app/components/workflow/constants.ts
- en/features/moderation_tool.md
- zh_CN/guides/application-design/moderation-tool.md
- api/tests/integration_tests/model_runtime/openai/test_moderation.py
- api/core/model_runtime/model_providers/openai/moderation/moderation.py
- api/tests/integration_tests/model_runtime/__mock/openai_moderation.py
- web/app/components/workflow/types.ts
- web/app/components/base/chat/types.ts
- web/app/components/workflow/nodes/_base/hooks/use-one-step-run.ts
- en/features/extension/api_based_extension/moderation-extension.md
- api/config.py
- en/getting-started/install-self-hosted/environments.md
About Dosu This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
To continue the conversation, mention @dosu.