loading...
No Results
  • Get Started
    • Welcome to GoInsight.AI
    • Quick Start
  • Knowledge 101
    • Key Concepts
    • Knowledge Base
    • LLM Selection Guide
    • Tool
    • Service
    • Data Security
  • Quick Chatbot
    • Build a Quick Bot
  • InsightFlow
    • InsightFlow Introduction
    • Types of InsightFlow
    • Node
      • Start
      • LLM
      • Knowledge Base Retrieval
      • Answer
      • Document Writing
      • Document Reading
      • HTTP Request
      • IF/ELSE
      • Question Classifier
      • Branch Aggregator
      • Multi branch Selector
      • Iteration
      • Auto-Continue
      • Template
      • Code
      • JSON Variable Extractor
      • Variable Assigner
      • Variable Extractor
      • KnowledgeFocus LLM
      • Agent
      • End
    • Publishing
      • Publishing an Interactive Flow
      • Publishing a Service Flow
      • Create Your First Workflow
  • Control & Management
    • Access Control
Home > Documentation > InsightFlow > Node

Code Node

Definition

The code node allows users to run Python code within a workflow to achieve customized data processing and implement complex logic.

code node

Quick Start/How to Use

Node Configuration Details: In ChatInsight.AI, right-click and select "Add Node" to add a 'Code' node to the workflow.

add code node

1. Configure the Input Variables Parameter for the Code Node

You can add new input variables by clicking the "+" button on the right. You must specify a name for each input variable and select the corresponding variable value from the workflow.

code input variables
Ensure that the data type of the input variables matches the type used in the code to avoid execution errors. For example, when performing mathematical operations in Python code, ensure that the input data type is a number (Number), otherwise, it will result in a type error.

2. Configure the Python3 Parameter for the Code Node

You can write Python code in the code editor to process and calculate the input variables. The code needs to define a main function that takes input variables as parameters and returns a dictionary as the output result.

code python 3
In Python, a dictionary is a data type used to store key-value pairs. It is unordered and mutable, and keys are unique, allowing for quick access to values via keys.
  • Inserted Variables
    If you want to reference a variable from the previous node while writing code, you can quickly insert it by entering
    {
    or
    /
    to open inserted variables.
  • AI Python debugger
    If you lack a Python background and find it challenging to write code, you can use the "AI Code" feature, which leverages AI capabilities to quickly generate high-quality Python code. To do so, the description in "your requirement" needs to be clear enough; the more detailed the steps, the better. You can then gradually refine the requirement description based on the change suggestions returned by AI coding and rewrite your applications multiple times until they run successfully.
  • ai code

3. Configure the Variable Output Mode Parameter for the Code Node

The "Variable Output Mode" offers two options: "Extract Field" and "Entire Output." The "Extract Field" option allows users to directly extract specific fields from the results of code as output variables, which can have one or multiple outputs. The "Entire Output" option allows users to output the entire dictionary from the code results into a single variable, resulting in only one output.

code variable output mode
When using the "Extract Field" mode, ensure that each key in the return statement corresponds to a configured output variable. If any keys returned by the code are not configured in the output variables, an error message will appear.

4. Configure the Iutput Variable Parameter for the Code Node

input variables

The Iutput Variable requires the mandatory input of an associated "Variable Name" and the selection of a "Data Type." The output "Variable Name" must match exactly with the keys in the return statement of the Python code to avoid errors caused by mismatches. The data types for Iutput variables in the Code node are as follows:

Data Type Description
String Used to store text information, such as usernames, addresses, etc.
Number Used to store integers or floating-point numbers for recording numerical data such as age or price.
Bool Boolean type, can only be true or false, commonly represents logical judgment results.
Array[Bool] An array that stores multiple boolean values, such as a collection of task completion statuses.
Array[Number] An array that stores multiple numeric values, such as product prices or quantities.
Array[Object] An array in which each element is an object that stores complex data structures, such as user information.
Array[String] An array that can contain multiple strings, such as multiple product names or city names.
Object Stores data in key-value pair format, such as detailed user information or order details.

Typical Use Cases:

  • String Concatenation
    • This example code concatenates two input variables, arg1 and arg2, and stores the result in the output variable "result". For instance, if arg1 is "Hello" and arg2 is "World", then the value of result will be "HelloWorld".
    • def main(arg1, arg2):
          return {
              "result": arg1 + arg2,
          }
      
    • In this example, the "Variable Output Mode" can optionally select "Extract Fields" to set the result as the output variable, with the data type chosen as String. The optional "Whole Output" will take the entire dictionary
      {"result": "HelloWorld"}
      0as the output variable, with the data type as Object.
  • Mathematical Calculation
    • This example code performs mathematical operations (including addition, subtraction, multiplication, and division) on two input variables, arg1 and arg2, and stores the results in the output variables "sum", "difference", "product", and "quotient", respectively. For instance, if arg1 is 10 and arg2 is 5, then sum will be 15, difference will be 5, product will be 50, and quotient will be 2.0.
    • def main(arg1, arg2):
          return {
              "sum": arg1 + arg2,
              "difference": arg1 - arg2,
              "product": arg1 * arg2,
              "quotient": arg1 / arg2,
          }
      
    • In this example, the "Variable Output Mode" can optionally select "Extract Fields" to set sum, difference, product, and quotient as the output variables, with the types being Number, respectively. The optional "Whole Output" will take the entire dictionary {"sum": 15, "difference": 5, "product": 50, "quotient": 2.0} as the output variable, with the type as Object.
  • Data Filtering
    • This example code takes a piece of input variable data, which is an array, and filters out the elements greater than 0 from it, storing the result in the output variable "filtered_data". For instance, if the data is [-1, 2, -3, 4, 5], then the value of filtered_data will be [2, 4].
    • def main(data):
          return {
              "filtered_data": [item for item in data if item > 0],
          }
      
    • In this example, the "Variable Output Mode" can optionally select "Extract Fields" to set filtered_data as the output variable, with the type being Array[Number]. The optional "Whole Output" will take the entire dictionary {"filtered_data": [2, 4]} as the output variable, with the type as Object.
  • Data Conversion
    • This example code receives a piece of input variable data, which is an array of strings, and converts each string in it to uppercase, storing the result in the output variable "converted_data". For instance, if the data is ["hello", "world"], then the value of converted_data will be ["HELLO", "WORLD"].
    • def main(data):
          return {
              "converted_data": [item.upper() for item in data],
          }
      
    • In this example, the "Variable Output Mode" can optionally select "Extract Fields" to set converted_data as the output variable, with the type being Array[String]. The optional "Whole Output" will take the entire dictionary {"converted_data": ["HELLO", "WORLD"]} as the output variable, with the type as Object.
  • Exception Handling
    • This example code performs a division operation on two input variables, arg1 and arg2, and stores the result in the output variable "result". If arg2 is 0, it catches the ZeroDivisionError exception and returns an error message stored in the output variable "error". For instance, if arg1 is 10 and arg2 is 0, the value of error will be "Division by zero is not allowed."
    • def main(arg1, arg2):
          try:
              result = arg1 / arg2
              return {
                  "result": result,
              }
          except ZeroDivisionError:
              return {
                  "error": "Division by zero is not allowed.",
              }
      
    • In this example, the "Variable Output Mode" can optionally set "Extract Fields" to designate result and error as output variables, with types Number and String, respectively. The optional "Whole Output" will output the entire dictionary {"result": 2.0} and {"error": "Division by zero is not allowed."} as output variables, with the type Object.

Practical Case Scenario:

code workflow

Here are the steps to extract comments from an Instagram video:

  • Start Node
    Create input variable (x) instagram id
  • Code Node
    Use the code node to preprocess the input Instagram ID, checking and removing any spaces and other symbols. If the user inputs a URL, the code should extract the ID from the URL for the next operation.
  • HTTP Request Node
    Use the HTTP node to search for comments on the corresponding Instagram video via the API.

This will allow code checks to be performed on the user-inputted Instagram ID, ensuring that the format of the ID remains correct at all times.

Notes:

  • Special limitations of the Python 3 interpreter
    Sandbox Environment: Only supports Python 3, can only use the Python standard library (e.g., datetime, json, re, etc.), and the requests library.
  • Cannot use other third-party libraries
    (e.g., pandas, numpy, etc.).
  • Cannot read or write local files
  • cannot save files to the server
  • cannot access databases

Frequently Asked Questions

Q1: How can I resolve a 'Main Key' error?
In the Input Variables section, if the parameter names in the Python code do not match the input parameter names defined in the workflow, simply change them to the same name.
Q2: How can I resolve an 'Error Missing' error?
If the error parameter is missing in the code, add the error output parameter with the type as String.
Q3: How is this different from using LLM large model nodes?
LLM large model nodes cannot handle complex problems, nor can they process multiple problems simultaneously, whereas code execution completes various tasks with strict coding requirements and can handle multiple complex operations at the same time.
Updated on: Jun 25, 2025
Prev Template
Next JSON Variable Extractor
On this page
  • Definition
  • Quick Start/How to Use
    • Input Variables
    • Python3
    • Variable Output Mode
    • Iutput Variable
  • Typical Use Cases
  • Practical Case Scenario
  • Notes
  • Frequently Asked Questions
loading...
No Results