Run Code
Controlled node
Overview
The Run Code node executes Python code in a sandboxed serverless environment. This node is useful for performing custom data processing, calculations, or transformations that aren't covered by other built-in nodes.
To pass data into your Python code, use #define variableName at the top of your code. The node will automatically create input sockets for each variable you define, allowing you to connect data from other nodes.
Inputs
| Input | Type | Description | Default |
|---|---|---|---|
| Run | Event | Triggers the execution of the Python code. | - |
| Code | Code | The Python code to execute. Access this via the properties panel. | See example below |
| Dynamic Inputs | Data | Additional inputs are automatically generated based on #define macros in your code. | - |
Dynamic Input Generation
The node scans your code for lines starting with #define and creates corresponding input sockets. For example:
#define inputData
#define multiplier
This creates two input sockets named inputData and multiplier that you can connect to other nodes.
Outputs
| Output | Type | Description |
|---|---|---|
| Done | Event | Fires when the code execution completes successfully. |
| Output | Data | Contains the return value from your Python code. If the code returns an error object, it will be passed through. |
Runtime Behavior and Defaults
Execution Environment
- Language: Python 3.x
- Timeout: 30 seconds (configurable via
MAX_CODE_EXECUTE_TIME_S). If your code exceeds this limit, aRunCodeErroris thrown. - Sandbox: Code runs in an isolated environment with access to standard Python libraries.
Error Handling
If your code raises an exception or times out:
- The node throws a
RunCodeErrorwith details about the failure - Execution stops and the workflow can handle the error via an Assert node or error handling connections
Return Values
Your Python code should return a value that will be passed to the Output socket. Supported return types include:
- Primitives (strings, numbers, booleans)
- Lists and dictionaries
- None (for operations that don't need to return data)
Example
Basic Calculation
#define price
#define tax_rate
# Calculate total with tax
total = price * (1 + tax_rate)
return total
Processing Input Data
#define raw_text
# Process the input text
words = raw_text.split()
word_count = len(words)
return {
"original_text": raw_text,
"word_count": word_count,
"words": words
}
Error Handling Pattern
Connect the Done event to an Assert node to check if the output contains an error:
#define data
try:
result = int(data) * 2
return result
except Exception as e:
return {"error": str(e)}
Then use an Assert node to check if the output is an error object and handle accordingly.