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

1def main(input_text, user_data):
2 # Your custom logic here
3 processed_text = input_text.upper()
4 result = {
5 "processed": processed_text,
6 "user_id": user_data.get("id", "unknown")
7 }
8 return result

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:

1# ❌ This will cause an error
2text = "Hello World"
3result = text.upper()

You’ll see this error:

NameError: name 'main' is not defined. Did you mean: 'min'?

Correct Structure

1# ✅ This is the correct structure
2def main(input_text):
3 result = input_text.upper()
4 return result

Code Execution Node Interface

The Code Execution Node provides a simple interface for configuring your custom code:

Code Execution Node

When you open the Code Execution Node, you’ll see a detailed code editor interface:

Code Execution Node Editor

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 be user_message
  • If your node has an input called chat_history, your function parameter must be chat_history
  • Parameter names are case-sensitive and must match exactly

Advanced Examples

Working with Chat History

1def main(chat_history, user_query):
2 # Process chat history
3 message_count = len(chat_history)
4 last_message = chat_history[-1] if chat_history else None
5
6 response = {
7 "query": user_query,
8 "context": f"Found {message_count} previous messages",
9 "last_message": last_message
10 }
11 return response

Using External Packages

1import json
2import datetime
3
4def main(data_input, timestamp):
5 # Process JSON data with timestamp
6 parsed_data = json.loads(data_input) if isinstance(data_input, str) else data_input
7
8 result = {
9 "processed_at": datetime.datetime.now().isoformat(),
10 "input_timestamp": timestamp,
11 "data": parsed_data
12 }
13 return result

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.