> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dify.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Code

> Execute custom Python or JavaScript for data processing

The Code node executes custom Python or JavaScript to handle complex data transformations, calculations, and logic within your workflow. Use it when preset nodes aren't sufficient for your specific processing needs.

![Code Node Configuration Interface](https://assets-docs.dify.ai/dify-enterprise-mintlify/en/guides/workflow/node/9969aa1bc1912aebe366f5d8f5dde296.png)

Code Node Configuration Interface

> [!NOTE]
>
> Code nodes need [`SANDBOX_ENABLE_NETWORK`](https://docs.dify.ai/en/self-host/deploy/configuration/environments#sandbox-service) set to `true`, the default. If it's `false`, every Code node fails with `network is disabled, please enable it in the configuration`.

## Configuration

Define **Input Variables** to access data from other nodes in your workflow, then reference these variables in your code. Your function must return a dictionary containing the **Output Variables** you've declared.

```python theme={null}
def main(input_variable: str) -> dict:
    # Process the input
    result = input_variable.upper()
    return {
        'output_variable': result
    }
```

## Language Support

Choose between **Python** and **JavaScript** based on your needs and familiarity. Both languages run in secure sandboxes with access to common libraries for data processing.

> [tab] Python
>
> Python includes standard libraries like `json`, `math`, `datetime`, and `re`. Ideal for data analysis, mathematical operations, and text processing.
>
> ```python theme={null}
> def main(data: list) -> dict:
>     import json
>     import math
>
>     average = sum(data) / len(data)
>     return {'result': math.ceil(average)}
> ```

> [tab] JavaScript
>
> JavaScript provides standard built-in objects and methods. Good for JSON manipulation and string operations.
>
> ```javascript theme={null}
> function main(data) {
>     const processed = data.map(item => item.toUpperCase());
>     return { result: processed };
> }
> ```

## Error Handling and Retries

Configure automatic retry behavior for failed code executions and define fallback strategies when code encounters errors.

![Error Handling Configuration Options](https://assets-docs.dify.ai/2024/12/58f392734ce44b22cd8c160faf28cd14.png)

Error Handling Configuration Options

**Retry on failure** allows up to 10 automatic retries with configurable intervals (maximum 5000ms). Enable this for handling temporary processing issues.

**Error Handling** lets you define fallback paths when code execution fails, allowing your workflow to continue running even when the code encounters problems.

![Retry Configuration Interface](https://assets-docs.dify.ai/2024/12/9fdd5525a91dc925b79b89272893becf.png)

Retry Configuration Interface

## Output Limits

An output that exceeds any of these limits fails the node:

| Output | Default limit | Variable |
| :- | :- | :- |
| String length | 400,000 characters | `CODE_MAX_STRING_LENGTH` |
| Numbers | ±9,223,372,036,854,775,807. Return larger numbers, such as very long IDs, as strings. | `CODE_MAX_NUMBER`, `CODE_MIN_NUMBER` |
| Decimal places | 20 | `CODE_MAX_PRECISION` |
| Object nesting depth | 5 levels | `CODE_MAX_DEPTH` |
| Items in an `array[string]` output | 30 | `CODE_MAX_STRING_ARRAY_LENGTH` |
| Items in an `array[object]` output | 30 | `CODE_MAX_OBJECT_ARRAY_LENGTH` |
| Items in an `array[number]` output | 1,000 | `CODE_MAX_NUMBER_ARRAY_LENGTH` |

With the default, an `object` output can hold three more levels of nested objects: the node's outputs count as the first level, and arrays don't add one.

If a result exceeds a limit, reduce the output or [raise the limit](https://docs.dify.ai/en/self-host/deploy/configuration/environments.md#code-and-template-output-limits).

## Security Considerations

Code in the sandbox can still reach other services inside your Dify deployment, so only run code you trust. To run code you don't, ask your deployment administrator to [isolate the sandbox's network](https://docs.dify.ai/en/self-host/deploy/configuration/environments.md#sandbox-service) first.

## Dependencies Support

Code nodes support external dependencies for both Python and JavaScript:

```python theme={null}
# Python: Import numpy, pandas, requests, etc.
import numpy as np
import pandas as pd

def main(data: list) -> dict:
    df = pd.DataFrame(data)
    return {'mean': float(np.mean(df['values']))}
```

```javascript theme={null}
// JavaScript: Import lodash, moment, etc.
const _ = require('lodash');

function main(data) {
    return { unique: _.uniq(data) };
}
```

Dependencies are pre-installed in the sandbox environment. Check the available packages list in your Dify installation.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.