Code Execution Node
The Code Execution Node empowers you to include custom logic defined directly in the workflow. You can even import custom public packages within the node’s logic.
Important: Code Execution Nodes expect a main()
function with parameters that match the names of the node’s inputs. If you don’t define a main()
function, you’ll encounter a NameError: name 'main' is not defined
error.
Code Structure Requirements
Your code must include a main()
function that serves as the entry point. The function parameters must match the input variable names defined in your node.
Basic Example
If your node has inputs named input_text
and user_data
, your main()
function must have parameters with exactly those names.
Common Error
If you write code without a main()
function:
You’ll see this error:
Correct Structure
Code Execution Node Interface
The Code Execution Node provides a simple interface for configuring your custom code:
When you open the Code Execution Node, you’ll see a detailed code editor interface:
Supported Languages
We support the following languages:
- Python
- TypeScript
Input Parameter Matching
The main()
function parameters must exactly match your node’s input names:
- If your node has an input called
user_message
, your function parameter must beuser_message
- If your node has an input called
chat_history
, your function parameter must bechat_history
- Parameter names are case-sensitive and must match exactly
Advanced Examples
Working with Chat History
Using External Packages
Troubleshooting
”NameError: name ‘main’ is not defined”
This error occurs when your code doesn’t include a main()
function. Always ensure your code has a main()
function as the entry point.
Parameter Name Mismatches
If your function parameters don’t match your node’s input names exactly, you may encounter unexpected behavior or errors. Double-check that parameter names match your node inputs precisely.