langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
43.07k stars 5.98k forks source link

Workflows created using customization cannot be published as tools, showing validation errors for ToolParameter #5192

Closed Sakura4036 closed 2 months ago

Sakura4036 commented 2 months ago

Self Checks

Dify version

0.6.10+ latest commit: ed53ef29f4bbe0333a131c408881b458c1d3be68

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

  1. Creating a workflow: using Google search tool
  2. Click on "Publish"
  3. Select "Publish as Tool"
  4. Fill in the tool information
  5. Click "Save" button

✔️ Expected Behavior

  1. save workflow as tool success

❌ Actual Behavior

image

dosubot[bot] commented 2 months ago

To resolve validation errors for ToolParameter when trying to publish workflows as tools in Dify version 0.6.10, ensure that the parameter types and values conform to the expected types defined in the ToolParameter class. Here are the supported parameter types and their corresponding expected values:

  1. STRING, SECRET_INPUT, SELECT:

    • Expected to be a string. If the value is None, it will be converted to an empty string.
  2. BOOLEAN:

    • Expected to be a boolean. If the value is None, it will be converted to False. If the value is a string, it should be one of the allowed YAML boolean value strings ('true', 'yes', 'y', '1' for True and 'false', 'no', 'n', '0' for False).
  3. NUMBER:

    • Expected to be an integer or a float. If the value is a string, it should be convertible to an integer or a float.
  4. FILE:

    • Expected to be a file path or identifier.

Ensure that your workflow parameters are validated and cast to the correct types before publishing the workflow as a tool. Additionally, the ToolProviderController class provides a method validate_parameters that can be used to validate the parameters of a tool. Here is an example of how you might use this method:

from core.tools.provider.tool_provider import ToolProviderController
from core.tools.entities.tool_entities import ToolParameter

# Example tool parameters
tool_parameters = {
    "param1": "123",
    "param2": True,
    "param3": "option1"
}

# Example tool name
tool_name = "example_tool"

# Create an instance of ToolProviderController
tool_provider_controller = ToolProviderController()

# Validate the parameters
try:
    tool_provider_controller.validate_parameters(tool_id=1, tool_name=tool_name, tool_parameters=tool_parameters)
    print("Parameters are valid")
except ToolParameterValidationError as e:
    print(f"Validation error: {e}")

Ensure that your tool configurations and parameters adhere to these validation rules to avoid errors when publishing workflows as tools [1][2][3][4].

To continue talking to Dosu, mention @dosu.

Sakura4036 commented 2 months ago

@dosu workfolw as tool works fine in Dify version 0.6.10, not in commit ed53ef29f4bbe0333a131c408881b458c1d3be68. I think this is a bug that needs to be fixed. I think this is a bug that needs to be fixed. And the workflow tools I published in version 0.6.10 are missing! image

dosubot[bot] commented 2 months ago

It seems like the validation errors for ToolParameter in the latest commit ed53ef29f4bbe0333a131c408881b458c1d3be68 are due to changes in how parameter configurations are validated. The check_parameter_configurations method in WorkflowToolConfigurationUtils raises a ValueError if any parameter configuration is invalid. This method checks each configuration by attempting to instantiate a WorkflowToolParameterConfiguration object with the given configuration dictionary. If the instantiation fails, it raises a ValueError.

To resolve this, you need to ensure that the parameter configurations are valid according to the WorkflowToolParameterConfiguration class. This involves verifying that all required fields are present and correctly formatted in the configuration dictionaries. Here is a code snippet to validate and correct the parameter configurations:

from core.tools.entities.tool_entities import WorkflowToolParameterConfiguration

def validate_and_correct_configurations(configurations: list[dict]) -> list[dict]:
    valid_configurations = []
    for configuration in configurations:
        try:
            # Attempt to create a WorkflowToolParameterConfiguration object
            WorkflowToolParameterConfiguration(**configuration)
            valid_configurations.append(configuration)
        except ValueError as e:
            # Handle the error or correct the configuration
            print(f"Invalid configuration: {configuration}, error: {e}")
            # Correct the configuration if possible
            corrected_configuration = correct_configuration(configuration)
            valid_configurations.append(corrected_configuration)
    return valid_configurations

def correct_configuration(configuration: dict) -> dict:
    # Implement your logic to correct the configuration
    # For example, add missing fields with default values
    corrected_configuration = configuration.copy()
    if 'required_field' not in corrected_configuration:
        corrected_configuration['required_field'] = 'default_value'
    return corrected_configuration

# Example usage
configurations = [
    # Your list of configuration dictionaries
]

valid_configurations = validate_and_correct_configurations(configurations)

Additionally, ensure that the parameters passed to the WorkflowTool class methods are correctly processed and validated. The _invoke method in the WorkflowTool class uses the _transform_args method to transform the tool parameters before invoking the workflow.

Here is an example of how the parameters are processed and added to the workflow_tool_provider object:

workflow_tool_provider = WorkflowToolProvider(
    tenant_id=tenant_id,
    user_id=user_id,
    app_id=workflow_app_id,
    name=name,
    label=label,
    icon=json.dumps(icon),
    description=description,
    parameter_configuration=json.dumps(parameters),
    privacy_policy=privacy_policy,
    version=workflow.version,
)

Ensure that all required fields such as name, label, icon, description, parameter_configuration, privacy_policy, and version are correctly populated. This approach will help you restore the missing workflow tools by ensuring all configurations are valid before publishing [1][2][3][4].

Since this issue might require a deeper look, @takatost could you please lend your expertise?

To continue talking to Dosu, mention @dosu.

Sakura4036 commented 2 months ago

Rolled back to version 0.6.10, everything is working fine and my workflow tools are back!

crazywoola commented 2 months ago

The main branch is not ready for prod, please keep using the latest v0.6.10.

Sakura4036 commented 2 months ago

yes, but this bug need to be fixed.

crazywoola commented 2 months ago

We are working on this.