Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to invoke tool: Failed to invoke tool: Output variable result[0] must be a string #15227

Open
5 tasks done
rainvstar opened this issue Mar 7, 2025 · 10 comments
Open
5 tasks done
Labels
🔨 feat:tools Tools for agent, function call related stuff.

Comments

@rainvstar
Copy link

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

1.0.0

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

Image
I hava a workflow, the output is Array[Object]。I publish this workflow as a tool。
when I use this tool in another workflow ,it goes wrong.

Image
Failed to invoke tool: Failed to invoke tool: Output variable result[0] must be a string

I don't understand how to deliever the output。

✔️ Expected Behavior

No response

❌ Actual Behavior

No response

Copy link

dosubot bot commented Mar 7, 2025

Hello, @rainvstar! I'm here to help you with your bug report and answer any questions you may have. Let's work together to resolve the issue you're facing.

To resolve the issue where the output variable result[0] must be a string, you need to transform the Array[Object] output into a text format. This is necessary because the Dify workflow output panel only supports text or file outputs directly.

You can achieve this transformation by using a template node or a code node. Here's an example of how you can use a code node to convert the array into a string:

def main(result: list):
    # Assuming result is a list of objects, convert each object to a string representation
    return {
        "result": "\n".join([str(item) for item in result])
    }

This script takes each object in the array, converts it to a string, and joins them with newline characters. Alternatively, you can use a template node to achieve a similar transformation [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 dosubot bot added the 🔨 feat:tools Tools for agent, function call related stuff. label Mar 7, 2025
@rainvstar
Copy link
Author

Hello, @rainvstar! I'm here to help you with your bug report and answer any questions you may have. Let's work together to resolve the issue you're facing.

To resolve the issue where the output variable result[0] must be a string, you need to transform the Array[Object] output into a text format. This is necessary because the Dify workflow output panel only supports text or file outputs directly.

You can achieve this transformation by using a template node or a code node. Here's an example of how you can use a code node to convert the array into a string:

def main(result: list):
# Assuming result is a list of objects, convert each object to a string representation
return {
"result": "\n".join([str(item) for item in result])
}
This script takes each object in the array, converts it to a string, and joins them with newline characters. Alternatively, you can use a template node to achieve a similar transformation [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

I return as string, but it still not work。

Image
Image
Image

@rainvstar
Copy link
Author

@dosubot @crazywoola
my python code:

import requests
import json

def main(sql: str) -> dict:
    url = 'http://host.docker.internal:5000/query'
    headers = {'Content-Type': 'application/json'}
    data = {'sql': sql}
    print('开始执行查询')
    response = requests.post(url, headers=headers, data=json.dumps(data))
    res = response.json()
    print(res)
    return {
        'result': "\n".join([str(item) for item in res])
    }
    # return {'result':sql}

result
{ "result": "{'group_id': 'group_001', 'group_name': '中美关系深度探讨群', 'group_nickname': '群主-张建国', 'join_time': 'Thu, 06 Mar 2025 16:00:01 GMT', 'wechat_id': 'user001'}\n{'group_id': 'group_001', 'group_name': '中美关系深度探讨群', 'group_nickname': '群员-李华', 'join_time': 'Thu, 06 Mar 2025 16:00:01 GMT', 'wechat_id': 'user002'}\n{'group_id': 'group_001', 'group_name': '中美关系深度探讨群', 'group_nickname': '群员-王芳', 'join_time': 'Thu, 06 Mar 2025 16:00:01 GMT', 'wechat_id': 'user003'}\n{'group_id': 'group_001', 'group_name': '中美关系深度探讨群', 'group_nickname': '群员-陈刚', 'join_time': 'Thu, 06 Mar 2025 16:00:01 GMT', 'wechat_id': 'user004'}\n{'group_id': 'group_001', 'group_name': '中美关系深度探讨群', 'group_nickname': '群员-孙阳', 'join_time': 'Thu, 06 Mar 2025 16:00:01 GMT', 'wechat_id': 'user100'}" }

I had convert the array into a string, but it still not work when i use this workflow as tool in another workflow.

Copy link

dosubot bot commented Mar 10, 2025

Ensure that the output variable result is correctly defined as a string in the workflow's output schema. The error "Output variable result[0] must be a string" suggests that the workflow expects a specific format for the output. Verify that the output schema in your workflow is set to expect a string type for the result variable. Here's an example of how you might define it:

output_schema:
  type: object
  properties:
    result:
      type: string

Additionally, make sure that the workflow where you are using this tool is also configured to handle the output as a string. If the issue persists, double-check that the output from your Python code is being correctly interpreted by the workflow as a string, and that there are no additional transformations or expectations in the subsequent workflow that might be causing the error [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

@rainvstar
Copy link
Author

@dosubot @crazywoola
this is my work flow, the definition of the output result type is string. I can' find where is the problem.

app:
  description: 输入sql,返回查询结果
  icon: 🤖
  icon_background: '#FFEAD5'
  mode: workflow
  name: Sql查询
  use_icon_as_answer_icon: false
kind: app
version: 0.1.5
workflow:
  conversation_variables: []
  environment_variables: []
  features:
    file_upload:
      allowed_file_extensions:
      - .JPG
      - .JPEG
      - .PNG
      - .GIF
      - .WEBP
      - .SVG
      allowed_file_types:
      - image
      allowed_file_upload_methods:
      - local_file
      - remote_url
      enabled: false
      fileUploadConfig:
        audio_file_size_limit: 50
        batch_count_limit: 5
        file_size_limit: 15
        image_file_size_limit: 10
        video_file_size_limit: 100
        workflow_file_upload_limit: 10
      image:
        enabled: false
        number_limits: 3
        transfer_methods:
        - local_file
        - remote_url
      number_limits: 3
    opening_statement: ''
    retriever_resource:
      enabled: true
    sensitive_word_avoidance:
      enabled: false
    speech_to_text:
      enabled: false
    suggested_questions: []
    suggested_questions_after_answer:
      enabled: false
    text_to_speech:
      enabled: false
      language: ''
      voice: ''
  graph:
    edges:
    - data:
        isInIteration: false
        sourceType: code
        targetType: end
      id: 1741254243163-source-1741593203734-target
      source: '1741254243163'
      sourceHandle: source
      target: '1741593203734'
      targetHandle: target
      type: custom
      zIndex: 0
    - data:
        isInIteration: false
        sourceType: start
        targetType: code
      id: 1741250705858-source-1741254243163-target
      source: '1741250705858'
      sourceHandle: source
      target: '1741254243163'
      targetHandle: target
      type: custom
      zIndex: 0
    nodes:
    - data:
        desc: ''
        selected: false
        title: 开始
        type: start
        variables:
        - label: sql
          max_length: 256
          options: []
          required: true
          type: text-input
          variable: sql
      height: 89
      id: '1741250705858'
      position:
        x: 304.49464086611715
        y: 328.84912107878586
      positionAbsolute:
        x: 304.49464086611715
        y: 328.84912107878586
      selected: false
      sourcePosition: right
      targetPosition: left
      type: custom
      width: 243
    - data:
        code: "import requests\nimport json\n\ndef main(sql: str) -> dict:\n    url\
          \ = 'http://host.docker.internal:5000/query'\n    headers = {'Content-Type':\
          \ 'application/json'}\n    data = {'sql': sql}\n    print('开始执行查询')\n  \
          \  response = requests.post(url, headers=headers, data=json.dumps(data))\n\
          \    res = response.json()\n    print(res)\n    return {\n        'result':\
          \ \"\\n\".join([str(item) for item in res])\n    }\n    # return {'result':sql}\n\
          \    "
        code_language: python3
        desc: ''
        outputs:
          result:
            children: null
            type: string
        selected: false
        title: 代码执行
        type: code
        variables:
        - value_selector:
          - '1741250705858'
          - sql
          variable: sql
      height: 53
      id: '1741254243163'
      position:
        x: 677.6039921567362
        y: 356.07478520348894
      positionAbsolute:
        x: 677.6039921567362
        y: 356.07478520348894
      selected: false
      sourcePosition: right
      targetPosition: left
      type: custom
      width: 243
    - data:
        desc: ''
        outputs:
        - value_selector:
          - '1741254243163'
          - result
          variable: result
        selected: false
        title: 结束
        type: end
      height: 89
      id: '1741593203734'
      position:
        x: 1088.2198683789743
        y: 356.07478520348894
      positionAbsolute:
        x: 1088.2198683789743
        y: 356.07478520348894
      selected: true
      sourcePosition: right
      targetPosition: left
      type: custom
      width: 243
    viewport:
      x: -271.54428042654354
      y: -151.83071563197592
      zoom: 1.1486983549970362

another workflow where to use the above tool

app:
  description: '23232'
  icon: 🤖
  icon_background: '#FFEAD5'
  mode: workflow
  name: test
  use_icon_as_answer_icon: false
kind: app
version: 0.1.5
workflow:
  conversation_variables: []
  environment_variables: []
  features:
    file_upload:
      allowed_file_extensions:
      - .JPG
      - .JPEG
      - .PNG
      - .GIF
      - .WEBP
      - .SVG
      allowed_file_types:
      - image
      allowed_file_upload_methods:
      - local_file
      - remote_url
      enabled: false
      fileUploadConfig:
        audio_file_size_limit: 50
        batch_count_limit: 5
        file_size_limit: 15
        image_file_size_limit: 10
        video_file_size_limit: 100
        workflow_file_upload_limit: 10
      image:
        enabled: false
        number_limits: 3
        transfer_methods:
        - local_file
        - remote_url
      number_limits: 3
    opening_statement: ''
    retriever_resource:
      enabled: true
    sensitive_word_avoidance:
      enabled: false
    speech_to_text:
      enabled: false
    suggested_questions: []
    suggested_questions_after_answer:
      enabled: false
    text_to_speech:
      enabled: false
      language: ''
      voice: ''
  graph:
    edges:
    - data:
        isInIteration: false
        sourceType: start
        targetType: tool
      id: 1741335806041-source-1741590655895-target
      source: '1741335806041'
      sourceHandle: source
      target: '1741590655895'
      targetHandle: target
      type: custom
      zIndex: 0
    - data:
        isInIteration: false
        sourceType: tool
        targetType: code
      id: 1741590655895--1741590681337-target
      source: '1741590655895'
      sourceHandle: source
      target: '1741590681337'
      targetHandle: target
      type: custom
      zIndex: 0
    - data:
        isInIteration: false
        sourceType: code
        targetType: end
      id: 1741590681337-source-1741590743086-target
      source: '1741590681337'
      sourceHandle: source
      target: '1741590743086'
      targetHandle: target
      type: custom
      zIndex: 0
    nodes:
    - data:
        desc: ''
        selected: false
        title: 开始
        type: start
        variables:
        - label: sql
          max_length: 200
          options: []
          required: true
          type: paragraph
          variable: sql
      height: 89
      id: '1741335806041'
      position:
        x: 142
        y: 168
      positionAbsolute:
        x: 142
        y: 168
      selected: false
      sourcePosition: right
      targetPosition: left
      type: custom
      width: 243
    - data:
        desc: ''
        provider_id: 46de781a-dafd-4093-bb10-59e15510980f
        provider_name: Sql查询服务
        provider_type: workflow
        selected: false
        title: Sql查询服务
        tool_configurations: {}
        tool_label: Sql查询服务
        tool_name: sqlQuery
        tool_parameters:
          sql:
            type: mixed
            value: '{{#1741335806041.sql#}}'
        type: tool
      height: 53
      id: '1741590655895'
      position:
        x: 441.96856686697913
        y: 168
      positionAbsolute:
        x: 441.96856686697913
        y: 168
      selected: false
      sourcePosition: right
      targetPosition: left
      type: custom
      width: 243
    - data:
        code: "\ndef main(text: str) -> dict:\n    return {\n        \"result\": text,\n\
          \    }\n"
        code_language: python3
        desc: ''
        outputs:
          result:
            children: null
            type: string
        selected: true
        title: 代码执行
        type: code
        variables:
        - value_selector:
          - '1741590655895'
          - text
          variable: text
      height: 53
      id: '1741590681337'
      position:
        x: 748
        y: 168
      positionAbsolute:
        x: 748
        y: 168
      selected: true
      sourcePosition: right
      targetPosition: left
      type: custom
      width: 243
    - data:
        desc: ''
        outputs:
        - value_selector:
          - '1741590655895'
          - text
          variable: text
        selected: false
        title: 结束
        type: end
      height: 89
      id: '1741590743086'
      position:
        x: 1051
        y: 168
      positionAbsolute:
        x: 1051
        y: 168
      selected: false
      sourcePosition: right
      targetPosition: left
      type: custom
      width: 243
    viewport:
      x: 82.24938427357768
      y: 199.67785567733603
      zoom: 0.6597539553864472

Copy link

dosubot bot commented Mar 10, 2025

Ensure that the output variable result is consistently defined as a string across both workflows. In your first workflow, the output is correctly defined as a string in the code node. However, in the second workflow, the tool node (Sql查询服务) should also be configured to expect a string output from the first workflow.

Check the tool_parameters in the second workflow to ensure that the output from the Sql查询服务 tool is being correctly mapped to a string variable. It seems like the tool_parameters might be incorrectly set, causing the output to be interpreted as an array instead of a string. Make sure that the output from the tool is directly assigned to a string variable in the subsequent nodes.

Additionally, verify that the tool_configurations and tool_parameters in the second workflow are correctly set to handle the string output from the first workflow. If the issue persists, double-check the data flow between nodes to ensure that the output is being correctly passed and interpreted as a string throughout the entire workflow.

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

@rainvstar
Copy link
Author

Ensure that the output variable result is consistently defined as a string across both workflows. In your first workflow, the output is correctly defined as a string in the code node. However, in the second workflow, the tool node (Sql查询服务) should also be configured to expect a string output from the first workflow.

Check the tool_parameters in the second workflow to ensure that the output from the Sql查询服务 tool is being correctly mapped to a string variable. It seems like the tool_parameters might be incorrectly set, causing the output to be interpreted as an array instead of a string. Make sure that the output from the tool is directly assigned to a string variable in the subsequent nodes.

Additionally, verify that the tool_configurations and tool_parameters in the second workflow are correctly set to handle the string output from the first workflow. If the issue persists, double-check the data flow between nodes to ensure that the output is being correctly passed and interpreted as a string throughout the entire workflow.

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 @crazywoola
In my second workflow ,I can't define the output type. how to define it as string.

Image

Copy link

dosubot bot commented Mar 10, 2025

To define the output type as a string in your second workflow when the "SQL查询服务" step outputs include "text" (String), you can use the VarItem component to specify the output variable with the type 'String'. Here is how you can define it:

<VarItem
  name='text'
  type='String'
  description={t(`${i18nPrefix}.outputVars.text`)}
/>

This code snippet shows how to define an output variable named 'text' with the type 'String', which matches the output type you want to use in your second workflow [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

@rainvstar
Copy link
Author

To define the output type as a string in your second workflow when the "SQL查询服务" step outputs include "text" (String), you can use the VarItem component to specify the output variable with the type 'String'. Here is how you can define it:

<VarItem
name='text'
type='String'
description={t(${i18nPrefix}.outputVars.text)}
/>
This code snippet shows how to define an output variable named 'text' with the type 'String', which matches the output type you want to use in your second workflow [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

I can't find VarItem Component. can you show me the picture? or give me a dsl then I can modify myself. I check my workflow and agent. Is it the definition of the workflow change? @dosubot @crazywoola

Copy link

dosubot bot commented Mar 12, 2025

The VarItem component is used in the Dify codebase to define and display variables, including their name, type, and description. However, it seems like you are looking for a way to define the output type directly in your workflow's configuration or DSL.

To define the output type as a string in your workflow, you should ensure that the output schema in your workflow configuration is correctly set to expect a string. Here's an example of how you might define it in your workflow's YAML configuration:

output_schema:
  type: object
  properties:
    result:
      type: string

This configuration specifies that the result output should be a string. Make sure that this matches the actual output from your code node and that the subsequent nodes in your workflow are configured to handle this string output correctly.

If you are using a visual interface or a DSL to define your workflow, ensure that the output variable is set to the correct type in the tool or node configuration. If the interface allows, you might need to manually set the output type to 'String' for the relevant node or tool.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 feat:tools Tools for agent, function call related stuff.
Projects
None yet
Development

No branches or pull requests

1 participant