Code node error using json.load: 'list' object has no attribute 'items' #16078

Closed
opened 2026-02-21 19:24:33 -05:00 by yindo · 7 comments
Owner

Originally created by @fade-color on GitHub (Aug 12, 2025).

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • 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, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.7.2

Cloud or Self Hosted

Cloud

Steps to reproduce

Add a Code node and fill in the following code:

import json

def main() -> dict:
    json_str = """{
    "tool_name": "generate_bars_lines_png",
    "tool_args": {
        "labels": ["1月", "2月", "3月", "4月", "5月", "6月"],
        "data_series": [
            [4344615, 4865504, 6162875, 6411734.1, 5564918.5, 5472399],
            [5236778, 3609031, 6198422.5, 6205801.5, 5417498.5, 4769855]
        ],
        "series_names": ["2025年收入", "2024年收入"],
        "x_label": "月份",
        "y_label": "收入(元)",
        "title": "2025年与2024年各月收入对比"
    }
}"""
    return {
        "json_obj": json.loads(json_str),
    }

✔️ Expected Behavior

Able to correctly parse the JSON string and return the data.

Actual Behavior

Running error: 'list' object has no attribute 'items'

Moreover, the input of this node cannot be seen; only the output is {}, and there is no more useful error information available.

Currently, only the above-mentioned specific JSON string can trigger this error. Other tool call functions do not trigger the error.

Image
Originally created by @fade-color on GitHub (Aug 12, 2025). ### Self Checks - [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542). - [x] This is only for bug report, if you would like to ask a question, 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, otherwise it will be closed. - [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version 1.7.2 ### Cloud or Self Hosted Cloud ### Steps to reproduce Add a `Code` node and fill in the following code: ```python import json def main() -> dict: json_str = """{ "tool_name": "generate_bars_lines_png", "tool_args": { "labels": ["1月", "2月", "3月", "4月", "5月", "6月"], "data_series": [ [4344615, 4865504, 6162875, 6411734.1, 5564918.5, 5472399], [5236778, 3609031, 6198422.5, 6205801.5, 5417498.5, 4769855] ], "series_names": ["2025年收入", "2024年收入"], "x_label": "月份", "y_label": "收入(元)", "title": "2025年与2024年各月收入对比" } }""" return { "json_obj": json.loads(json_str), } ``` ### ✔️ Expected Behavior Able to correctly parse the JSON string and return the data. ### ❌ Actual Behavior Running error: 'list' object has no attribute 'items' Moreover, the input of this node cannot be seen; only the output is `{}`, and there is no more useful error information available. Currently, only the above-mentioned specific JSON string can trigger this error. Other tool call functions do not trigger the error. <img width="2794" height="1330" alt="Image" src="https://github.com/user-attachments/assets/5ea9f469-872b-49ce-838f-0ed938bf5ba5" />
yindo added the 🐞 bug label 2026-02-21 19:24:33 -05:00
yindo closed this issue 2026-02-21 19:24:33 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 12, 2025):

Hi @fade-color! I'm Dosu and I’m helping the dify team.

The error happens because Dify expects the output of your main() function to be a dictionary whose structure matches the output schema defined for the Code node. If the returned value is a list, or if a nested value is a list where a dict is expected, Dify's internal validation tries to call .items() on a list, causing this error. This is not caused by json.loads itself, but by Dify's strict output validation logic after your code runs [source].

To fix this, make sure the output schema for your Code node matches the structure of the dictionary you return—including all nested arrays and objects. If you want to return the parsed JSON as a single object, ensure the output schema has a key (like json_obj) of type object, and that you return a dict with that key.

If you still see issues, a common workaround is to serialize the output with json.dumps before returning it, so Dify treats it as a string instead of trying to validate the nested structure [source].

Example that should work:

import json

def main() -> dict:
    json_str = """{
        "tool_name": "generate_bars_lines_png",
        "tool_args": {
            "labels": ["1月", "2月", "3月", "4月", "5月", "6月"],
            "data_series": [
                [4344615, 4865504, 6162875, 6411734.1, 5564918.5, 5472399],
                [5236778, 3609031, 6198422.5, 6205801.5, 5417498.5, 4769855]
            ],
            "series_names": ["2025年收入", "2024年收入"],
            "x_label": "月份",
            "y_label": "收入(元)",
            "title": "2025年与2024年各月收入对比"
        }
    }"""
    # Option 1: Return as object (preferred, if schema matches)
    return {
        "json_obj": json.loads(json_str)
    }
    # Option 2: If you still get errors, return as string:
    # return {
    #     "json_obj": json_str
    # }

If your output schema expects json_obj to be an object, Option 1 should work. If it expects a string, use Option 2.

If your JSON contains deeply nested lists or multi-dimensional arrays, make sure the schema reflects this, as Dify is strict about type and structure matching for all nested fields [source].

If you need more help aligning your schema, share your Code node's output schema configuration.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Aug 12, 2025): <!-- Greeting --> Hi @fade-color! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the dify team. <!-- Answer --> The error happens because Dify expects the output of your `main()` function to be a dictionary whose structure matches the output schema defined for the Code node. If the returned value is a list, or if a nested value is a list where a dict is expected, Dify's internal validation tries to call `.items()` on a list, causing this error. This is not caused by `json.loads` itself, but by Dify's strict output validation logic after your code runs [[source]](https://github.com/langgenius/dify/blob/de0dae9d9b98e5d39d672024c5bb43c721a7fcd6/api/core/workflow/nodes/code/code_node.py). To fix this, make sure the output schema for your Code node matches the structure of the dictionary you return—including all nested arrays and objects. If you want to return the parsed JSON as a single object, ensure the output schema has a key (like `json_obj`) of type `object`, and that you return a dict with that key. If you still see issues, a common workaround is to serialize the output with `json.dumps` before returning it, so Dify treats it as a string instead of trying to validate the nested structure [[source]](https://github.com/langgenius/dify/issues/20316). Example that should work: ```python import json def main() -> dict: json_str = """{ "tool_name": "generate_bars_lines_png", "tool_args": { "labels": ["1月", "2月", "3月", "4月", "5月", "6月"], "data_series": [ [4344615, 4865504, 6162875, 6411734.1, 5564918.5, 5472399], [5236778, 3609031, 6198422.5, 6205801.5, 5417498.5, 4769855] ], "series_names": ["2025年收入", "2024年收入"], "x_label": "月份", "y_label": "收入(元)", "title": "2025年与2024年各月收入对比" } }""" # Option 1: Return as object (preferred, if schema matches) return { "json_obj": json.loads(json_str) } # Option 2: If you still get errors, return as string: # return { # "json_obj": json_str # } ``` If your output schema expects `json_obj` to be an object, Option 1 should work. If it expects a string, use Option 2. If your JSON contains deeply nested lists or multi-dimensional arrays, make sure the schema reflects this, as Dify is strict about type and structure matching for all nested fields [[source]](https://github.com/langgenius/dify/issues/15498). If you need more help aligning your schema, share your Code node's output schema configuration. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/23e36a95-df5d-43a3-a4aa-a6a203ca8685?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/23e36a95-df5d-43a3-a4aa-a6a203ca8685?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/23e36a95-df5d-43a3-a4aa-a6a203ca8685?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/23e36a95-df5d-43a3-a4aa-a6a203ca8685?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/23e36a95-df5d-43a3-a4aa-a6a203ca8685?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/23e36a95-df5d-43a3-a4aa-a6a203ca8685?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/23e36a95-df5d-43a3-a4aa-a6a203ca8685?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/23798)
Author
Owner

@fade-color commented on GitHub (Aug 12, 2025):

Hi @fade-color! I'm Dosu and I’m helping the dify team.

The error happens because Dify expects the output of your main() function to be a dictionary whose structure matches the output schema defined for the Code node. If the returned value is a list, or if a nested value is a list where a dict is expected, Dify's internal validation tries to call .items() on a list, causing this error. This is not caused by json.loads itself, but by Dify's strict output validation logic after your code runs [source].

To fix this, make sure the output schema for your Code node matches the structure of the dictionary you return—including all nested arrays and objects. If you want to return the parsed JSON as a single object, ensure the output schema has a key (like json_obj) of type object, and that you return a dict with that key.

If you still see issues, a common workaround is to serialize the output with json.dumps before returning it, so Dify treats it as a string instead of trying to validate the nested structure [source].

Example that should work:

import json

def main() -> dict:
json_str = """{
"tool_name": "generate_bars_lines_png",
"tool_args": {
"labels": ["1月", "2月", "3月", "4月", "5月", "6月"],
"data_series": [
[4344615, 4865504, 6162875, 6411734.1, 5564918.5, 5472399],
[5236778, 3609031, 6198422.5, 6205801.5, 5417498.5, 4769855]
],
"series_names": ["2025年收入", "2024年收入"],
"x_label": "月份",
"y_label": "收入(元)",
"title": "2025年与2024年各月收入对比"
}
}"""
# Option 1: Return as object (preferred, if schema matches)
return {
"json_obj": json.loads(json_str)
}
# Option 2: If you still get errors, return as string:
# return {
# "json_obj": json_str
# }
If your output schema expects json_obj to be an object, Option 1 should work. If it expects a string, use Option 2.

If your JSON contains deeply nested lists or multi-dimensional arrays, make sure the schema reflects this, as Dify is strict about type and structure matching for all nested fields [source].

If you need more help aligning your schema, share your Code node's output schema configuration.

To reply, just mention @dosu.

How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

This error seems to be caused by a nested list within a certain field in the JSON string. When I access the tool_args.data_series field, the code runs normally. However, the JSON string is allowed to have nested lists. @duso

@fade-color commented on GitHub (Aug 12, 2025): > Hi [@fade-color](https://github.com/fade-color)! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the dify team. > > The error happens because Dify expects the output of your `main()` function to be a dictionary whose structure matches the output schema defined for the Code node. If the returned value is a list, or if a nested value is a list where a dict is expected, Dify's internal validation tries to call `.items()` on a list, causing this error. This is not caused by `json.loads` itself, but by Dify's strict output validation logic after your code runs [[source]](https://github.com/langgenius/dify/blob/de0dae9d9b98e5d39d672024c5bb43c721a7fcd6/api/core/workflow/nodes/code/code_node.py). > > To fix this, make sure the output schema for your Code node matches the structure of the dictionary you return—including all nested arrays and objects. If you want to return the parsed JSON as a single object, ensure the output schema has a key (like `json_obj`) of type `object`, and that you return a dict with that key. > > If you still see issues, a common workaround is to serialize the output with `json.dumps` before returning it, so Dify treats it as a string instead of trying to validate the nested structure [[source]](https://github.com/langgenius/dify/issues/20316). > > Example that should work: > > import json > > def main() -> dict: > json_str = """{ > "tool_name": "generate_bars_lines_png", > "tool_args": { > "labels": ["1月", "2月", "3月", "4月", "5月", "6月"], > "data_series": [ > [4344615, 4865504, 6162875, 6411734.1, 5564918.5, 5472399], > [5236778, 3609031, 6198422.5, 6205801.5, 5417498.5, 4769855] > ], > "series_names": ["2025年收入", "2024年收入"], > "x_label": "月份", > "y_label": "收入(元)", > "title": "2025年与2024年各月收入对比" > } > }""" > # Option 1: Return as object (preferred, if schema matches) > return { > "json_obj": json.loads(json_str) > } > # Option 2: If you still get errors, return as string: > # return { > # "json_obj": json_str > # } > If your output schema expects `json_obj` to be an object, Option 1 should work. If it expects a string, use Option 2. > > If your JSON contains deeply nested lists or multi-dimensional arrays, make sure the schema reflects this, as Dify is strict about type and structure matching for all nested fields [[source]](https://github.com/langgenius/dify/issues/15498). > > If you need more help aligning your schema, share your Code node's output schema configuration. > > _To reply, just mention [@dosu](https://go.dosu.dev/dosubot)._ > > How did I do? [Good](https://app.dosu.dev/response-feedback/23e36a95-df5d-43a3-a4aa-a6a203ca8685?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/23e36a95-df5d-43a3-a4aa-a6a203ca8685?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/23e36a95-df5d-43a3-a4aa-a6a203ca8685?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/23e36a95-df5d-43a3-a4aa-a6a203ca8685?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/23e36a95-df5d-43a3-a4aa-a6a203ca8685?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/23e36a95-df5d-43a3-a4aa-a6a203ca8685?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/23e36a95-df5d-43a3-a4aa-a6a203ca8685?feedback_type=other)  [![Chat with Dosu](https://camo.githubusercontent.com/d9ffebb979729c9ef0fc4e3090dbae5b17cabbfec8afe378ada6b81913d6ad8a/68747470733a2f2f646f73752e6465762f646f73752d636861742d62616467652e737667)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github) [![Join Discord](https://camo.githubusercontent.com/d562c2df5e4567ea41d37f1d91a4da0839d8341037045b103b2da268998de17f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6a6f696e2d3538363546323f6c6f676f3d646973636f7264266c6f676f436f6c6f723d7768697465266c6162656c3d)](https://go.dosu.dev/discord-bot) [![Share on X](https://camo.githubusercontent.com/4649f705e2396d6f328dbc8e800a0509c9c17b3b20253b36706c393df124b93d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f582d73686172652d626c61636b)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/23798) This error seems to be caused by a nested list within a certain field in the JSON string. When I access the `tool_args.data_series` field, the code runs normally. However, the JSON string is allowed to have nested lists. @duso
Author
Owner

@dosubot[bot] commented on GitHub (Aug 12, 2025):

This error happens because Dify strictly validates the structure and types of your Code node output against the output schema, including all nested arrays and objects. If the schema expects an object but encounters a list (or vice versa) at any level, you'll get errors like 'list' object has no attribute 'items'—this is not caused by json.loads, but by Dify's recursive output validation logic after your code runs (source).

To support nested lists or multi-dimensional arrays, your output schema must exactly match the structure of your returned data, including specifying arrays of arrays where needed. For example, if you have a field like data_series: [[...], [...]], the schema for data_series must be set as an array of arrays of numbers.

If aligning the schema is too complex or you still get validation errors, you can serialize your output with json.dumps and return it as a string. This bypasses deep validation, since Dify treats strings as atomic values and doesn't recursively check their structure.

If you want more specific guidance, please share your Code node's output schema configuration.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Aug 12, 2025): <!-- Answer --> This error happens because Dify strictly validates the structure and types of your Code node output against the output schema, including all nested arrays and objects. If the schema expects an object but encounters a list (or vice versa) at any level, you'll get errors like `'list' object has no attribute 'items'`—this is not caused by `json.loads`, but by Dify's recursive output validation logic after your code runs ([source](https://github.com/langgenius/dify/blob/de0dae9d9b98e5d39d672024c5bb43c721a7fcd6/api/core/workflow/nodes/code/code_node.py)). To support nested lists or multi-dimensional arrays, your output schema must exactly match the structure of your returned data, including specifying arrays of arrays where needed. For example, if you have a field like `data_series: [[...], [...]]`, the schema for `data_series` must be set as an array of arrays of numbers. If aligning the schema is too complex or you still get validation errors, you can serialize your output with `json.dumps` and return it as a string. This bypasses deep validation, since Dify treats strings as atomic values and doesn't recursively check their structure. If you want more specific guidance, please share your Code node's output schema configuration. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/c36e5614-bd08-4fbc-8950-5047459620a5?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/c36e5614-bd08-4fbc-8950-5047459620a5?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/c36e5614-bd08-4fbc-8950-5047459620a5?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/c36e5614-bd08-4fbc-8950-5047459620a5?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/c36e5614-bd08-4fbc-8950-5047459620a5?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/c36e5614-bd08-4fbc-8950-5047459620a5?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/c36e5614-bd08-4fbc-8950-5047459620a5?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/23798)
Author
Owner

@fade-color commented on GitHub (Aug 12, 2025):

This error happens because Dify strictly validates the structure and types of your Code node output against the output schema, including all nested arrays and objects. If the schema expects an object but encounters a list (or vice versa) at any level, you'll get errors like 'list' object has no attribute 'items'—this is not caused by json.loads, but by Dify's recursive output validation logic after your code runs (source).

To support nested lists or multi-dimensional arrays, your output schema must exactly match the structure of your returned data, including specifying arrays of arrays where needed. For example, if you have a field like data_series: [[...], [...]], the schema for data_series must be set as an array of arrays of numbers.

If aligning the schema is too complex or you still get validation errors, you can serialize your output with json.dumps and return it as a string. This bypasses deep validation, since Dify treats strings as atomic values and doesn't recursively check their structure.

If you want more specific guidance, please share your Code node's output schema configuration.

To reply, just mention @dosu.

How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

Indeed, that's correct. Thank you very much. If I want to load this data into a variable, how should I proceed correctly? @dosu

@fade-color commented on GitHub (Aug 12, 2025): > This error happens because Dify strictly validates the structure and types of your Code node output against the output schema, including all nested arrays and objects. If the schema expects an object but encounters a list (or vice versa) at any level, you'll get errors like `'list' object has no attribute 'items'`—this is not caused by `json.loads`, but by Dify's recursive output validation logic after your code runs ([source](https://github.com/langgenius/dify/blob/de0dae9d9b98e5d39d672024c5bb43c721a7fcd6/api/core/workflow/nodes/code/code_node.py)). > > To support nested lists or multi-dimensional arrays, your output schema must exactly match the structure of your returned data, including specifying arrays of arrays where needed. For example, if you have a field like `data_series: [[...], [...]]`, the schema for `data_series` must be set as an array of arrays of numbers. > > If aligning the schema is too complex or you still get validation errors, you can serialize your output with `json.dumps` and return it as a string. This bypasses deep validation, since Dify treats strings as atomic values and doesn't recursively check their structure. > > If you want more specific guidance, please share your Code node's output schema configuration. > > _To reply, just mention [@dosu](https://go.dosu.dev/dosubot)._ > > How did I do? [Good](https://app.dosu.dev/response-feedback/c36e5614-bd08-4fbc-8950-5047459620a5?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/c36e5614-bd08-4fbc-8950-5047459620a5?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/c36e5614-bd08-4fbc-8950-5047459620a5?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/c36e5614-bd08-4fbc-8950-5047459620a5?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/c36e5614-bd08-4fbc-8950-5047459620a5?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/c36e5614-bd08-4fbc-8950-5047459620a5?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/c36e5614-bd08-4fbc-8950-5047459620a5?feedback_type=other)  [![Chat with Dosu](https://camo.githubusercontent.com/d9ffebb979729c9ef0fc4e3090dbae5b17cabbfec8afe378ada6b81913d6ad8a/68747470733a2f2f646f73752e6465762f646f73752d636861742d62616467652e737667)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github) [![Join Discord](https://camo.githubusercontent.com/d562c2df5e4567ea41d37f1d91a4da0839d8341037045b103b2da268998de17f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6a6f696e2d3538363546323f6c6f676f3d646973636f7264266c6f676f436f6c6f723d7768697465266c6162656c3d)](https://go.dosu.dev/discord-bot) [![Share on X](https://camo.githubusercontent.com/4649f705e2396d6f328dbc8e800a0509c9c17b3b20253b36706c393df124b93d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f582d73686172652d626c61636b)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/23798) Indeed, that's correct. Thank you very much. If I want to load this data into a variable, how should I proceed correctly? @dosu
Author
Owner

@dosubot[bot] commented on GitHub (Aug 12, 2025):

To load the output data from a Dify Code node into a variable, you have two main scenarios:

  1. If you return a Python object (dict) and your output schema matches the structure:
    You can access the output directly as a Python object in downstream nodes. For example, if your output is {"json_obj": parsed_data}, you can use json_obj as a dict in the next node.

  2. If you serialize the output with json.dumps to bypass strict validation:
    Your output will be a JSON string. To use it as a Python object later, load it with json.loads. For example:

    import json
    # Suppose output['json_obj'] is a string
    data = json.loads(output['json_obj'])
    # Now 'data' is a Python dict or list, ready for further processing
    

Serialization (json.dumps) is a recommended workaround in Dify for complex or deeply nested outputs, especially when schema alignment is difficult or unnecessary. Just remember to use json.loads to deserialize the string if you need to work with the data as a Python object in later steps or nodes [source].

If you want more tailored advice, feel free to share your downstream use case or output schema!

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Aug 12, 2025): <!-- Answer --> To load the output data from a Dify Code node into a variable, you have two main scenarios: 1. **If you return a Python object (dict) and your output schema matches the structure:** You can access the output directly as a Python object in downstream nodes. For example, if your output is `{"json_obj": parsed_data}`, you can use `json_obj` as a dict in the next node. 2. **If you serialize the output with `json.dumps` to bypass strict validation:** Your output will be a JSON string. To use it as a Python object later, load it with `json.loads`. For example: ```python import json # Suppose output['json_obj'] is a string data = json.loads(output['json_obj']) # Now 'data' is a Python dict or list, ready for further processing ``` Serialization (`json.dumps`) is a recommended workaround in Dify for complex or deeply nested outputs, especially when schema alignment is difficult or unnecessary. Just remember to use `json.loads` to deserialize the string if you need to work with the data as a Python object in later steps or nodes [[source]](https://github.com/langgenius/dify/blob/de0dae9d9b98e5d39d672024c5bb43c721a7fcd6/api/core/workflow/nodes/code/code_node.py). If you want more tailored advice, feel free to share your downstream use case or output schema! <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/dfb47b9e-1ff5-4dc3-8f4f-a307909af7f9?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/dfb47b9e-1ff5-4dc3-8f4f-a307909af7f9?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/dfb47b9e-1ff5-4dc3-8f4f-a307909af7f9?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/dfb47b9e-1ff5-4dc3-8f4f-a307909af7f9?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/dfb47b9e-1ff5-4dc3-8f4f-a307909af7f9?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/dfb47b9e-1ff5-4dc3-8f4f-a307909af7f9?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/dfb47b9e-1ff5-4dc3-8f4f-a307909af7f9?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/23798)
Author
Owner

@fade-color commented on GitHub (Aug 12, 2025):

Okay, thank you very much. I will change my usage method.

@fade-color commented on GitHub (Aug 12, 2025): Okay, thank you very much. I will change my usage method.
Author
Owner

@dimsky commented on GitHub (Nov 20, 2025):

+1

@dimsky commented on GitHub (Nov 20, 2025): +1
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#16078