All guides

Dify label cleanup: verify a Code node’s return values

Normalize a label array, declare its outputs, and check types, the 500-item limit and failure routing.

AINavWritten / Updated Sources checked
On this page

At a glance

Generated labels can contain whitespace, duplicate values or inconsistent case. This task gives a teaching label array to a Dify Code node and returns a lowercase list plus its count. It starts with an existing array and ends with two output variables that downstream nodes can read. Deduplication is handled by Python; no model call was made for this article.

Task flow

  1. Input binding

    Bind the array as labels and check that its elements are strings.

  2. Python transformation

    Check size and types, trim whitespace, lowercase, and deduplicate in first-seen order.

  3. Output contract

    Return normalized and count in a dictionary matching the declared output variables.

The arrows show data and checks for this task; branches are explained in the text.

Fix the input and outputs first

Prepare the teaching array [" Billing ", "billing", "Bug", ""] in an existing workflow. It represents labels that need cleanup after classification. Define labels in the Code node’s Input Variables and bind that array. Declare normalized as a string array and count as a number in Output Variables. The return keys must match these declarations; returning an undeclared key does not complete the contract.

Enter Python in the Code node

Select Python and place this code in the node editor. The main parameter corresponds to the input variable and the returned dictionary to the output variables. Empty values are dropped, and duplicates retain their first position. Under this teaching rule “Bug” and “bug” are the same label. Confirm that rule fits your taxonomy; do not apply it to identifiers whose case matters.

Example: normalize labels and return their count
python
def main(labels: list) -> dict:
    if not isinstance(labels, list) or len(labels) > 500:
        raise ValueError("Expected at most 500 labels")
    normalized = []
    seen = set()
    for label in labels:
        if not isinstance(label, str):
            raise ValueError("Every label must be a string")
        value = label.strip().lower()
        if value and value not in seen:
            seen.add(value)
            normalized.append(value)
    return {"normalized": normalized, "count": len(normalized)}

Compare the input and output

Result derived from the teaching input
CheckExpected value
Input labels[" Billing ", "billing", "Bug", ""]
Output normalized["billing", "bug"]
Output count2
Declared outputsBoth names and types match the returned dictionary

The values follow from Python logic, not an observed Dify execution. When you debug the node, inspect its actual names and types as well as its displayed data. A string containing JSON array text is still a string and does not satisfy an array[string] output.

Check limits before returning

The official page limits array outputs to 500 items. The example rejects larger inputs before processing rather than silently discarding their tail. The page also limits strings to 400000 characters and specifies number and object nesting limits. This example returns a flat string array and an integer; unusually long individual labels still need attention. Check the declared limits before expanding the transformation.

Separate invalid input from execution failure

A non-array input, more than 500 labels or a non-string element raises ValueError. Correct the upstream input before downstream processing. Code supports retries and error paths, but repeating the same invalid input does not repair it. Consider retry for a transient execution failure. If the workflow must continue, configure an explicit failure path that preserves the fact that no valid list was obtained.

Completion and verification

  • The bound input is an actual array and the parameter matches the code; both return keys have declared outputs.
  • The observed output matches the example, and first-seen order, empty filtering and case rules fit the task.
  • Invalid inputs follow the failure path rather than becoming a valid empty list.
  • The code reads no files, executes no system commands and uses built-in Python operations without extra dependencies.

The official Code documentation was read to check interfaces, sandbox behavior and limits. No Dify login, workflow creation or node execution occurred. The example uses built-in Python logic. Verify the outputs and failure route in your own Dify environment before using it downstream.

Article sources

Original editorial content; official descriptions and checking suggestions are distinguished.

  1. Dify · Official sources
    View details