dify icon indicating copy to clipboard operation
dify copied to clipboard

Custom plugin tool with `output_schema` error

Open majinkai opened this issue 10 months ago • 8 comments

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

Dify version

1.0.1

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

output_schema:
  type: object
  properties:
    text:
      type: string

✔️ Expected Behavior

python -m main {"event": "log", "data": {"level": "INFO", "message": "Installed tool: union-id", "timestamp": 1742346828.017111}}

❌ Actual Behavior

python -m main

An error occurred while parsing the data: b"tools register failed, invalid tools declaration: Key: 'ToolProviderDeclaration.Tools[0].OutputSchema' Error:Field validation for 'OutputSchema' failed on the 'json_schema' tag"
Traceback (most recent call last):
  File "/Users/majinkai/Desktop/dify-build/union-id/venv/lib/python3.12/site-packages/dify_plugin/core/server/tcp/request_reader.py", line 147, in _read_stream
    data = loads(line)
           ^^^^^^^^^^^

majinkai avatar Mar 19 '25 01:03 majinkai

Hey @majinkai! I'm here to help you with your bug report and questions. Let's work together to solve the issue you're facing.

The error message indicates that there is a problem with the output_schema validation in your tool declaration. The output_schema should be defined using the json_schema format, which you have done correctly in your YAML configuration. However, the error suggests that there might be an issue with how the schema is being processed or validated in the code.

Here are a few things you can check or try:

  1. Ensure Correct Format: Double-check that your output_schema is correctly formatted according to the JSON schema standards. Your example seems correct, but ensure there are no hidden formatting issues.

  2. Check Implementation: Make sure that the tool's implementation returns a variable that matches the defined schema. If the tool does not return a variable corresponding to the schema, it will result in a None return value [1][2].

  3. Validation Rules: The validation rules applied to the output_schema involve checking the type and properties defined in the schema. Ensure that these are correctly specified and that the tool's output matches these specifications [3][4].

  4. Default Structure: If the output_schema is not provided or incorrectly defined, a default structure (TOOL_OUTPUT_STRUCT) might be used. Ensure that your schema is correctly defined to avoid falling back to defaults [5].

If these checks do not resolve the issue, there might be a deeper problem in the code handling the schema validation. You may need to look into the specific implementation details or error handling mechanisms in the Dify codebase related to json_schema validation.

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

dosubot[bot] avatar Mar 19 '25 01:03 dosubot[bot]

How should I operate if I want to override default structure(TOOL_OUTPUT_STRUCT) and only output one variable @dosu

majinkai avatar Mar 19 '25 01:03 majinkai

To override the default structure (TOOL-OUTPUT-STRUCT) and output only one variable, you need to modify the logic within the BlockEnum.Tool case. Specifically, adjust the assignment to res.vars to include only the desired variable from the output_schema. Here's an example:

case BlockEnum.Tool: {
  const {
    output_schema,
  } = data as ToolNodeType
  if (!output_schema) {
    res.vars = TOOL_OUTPUT_STRUCT
  }
  else {
    const outputSchema: any[] = []
    const desiredVariableKey = 'yourDesiredVariableKey'; // Replace with the key of the variable you want to output
    if (output_schema.properties[desiredVariableKey]) {
      const output = output_schema.properties[desiredVariableKey]
      outputSchema.push({
        variable: desiredVariableKey,
        type: output.type === 'array'
          ? `array[${output.items?.type.slice(0, 1).toLocaleLowerCase()}${output.items?.type.slice(1)}]`
          : `${output.type.slice(0, 1).toLocaleLowerCase()}${output.type.slice(1)}`,
        description: output.description,
      })
    }
    res.vars = outputSchema
  }
  break
}

Replace 'yourDesiredVariableKey' with the actual key of the variable you want to output. This code ensures that only the specified variable is included in the res.vars array [1].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

dosubot[bot] avatar Mar 19 '25 01:03 dosubot[bot]

I don't want to change any source code, I just want to constrain the return of only one variable through settings. @dosu

majinkai avatar Mar 19 '25 02:03 majinkai

You can constrain the return to only one variable through configuration settings by defining an output_schema using the json_schema format. Here's an example of how to specify a single variable in the output_schema:

output_schema:
  type: object
  properties:
    name:
      type: string

This configuration ensures that only the name variable is included in the output. Make sure to set additionalProperties: false to prevent any other variables from being included:

output_schema:
  type: object
  properties:
    name:
      type: string
  additionalProperties: false

This setup will strictly limit the output to the specified variable [1].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

dosubot[bot] avatar Mar 19 '25 02:03 dosubot[bot]

It is necessary to include not only variables but also a description in i18n format.

dar0xt avatar Mar 27 '25 23:03 dar0xt

output_schema:
  type: object
  properties:
    dataset_id:
      type: string
      label:
        en_US: Dataset Id
        zh_Hans: 知识库ID
      human_description:
        en_US: Dataset Id
        zh_Hans: 知识库ID

Image

Can I hide variables other than dataset_id. Additionally, dataset_id failed to internationalize.

majinkai avatar Mar 28 '25 05:03 majinkai

@majinkai Where is the definition of output_sheme? I use dify 1.2.0, and define output_shecme in /tools/xxx.ymal, but didn't work! Thansk!

logzh519 avatar Apr 27 '25 14:04 logzh519