Page cover image

Code Node

The code node is used to execute code within your workflows and provides full flexibility to connect to systems which aren't built-in integrations and to execute any custom functions.

Python Environment and Custom Packages

The code node provides a python environment and includes standard request-handling and commonly used libraries. It currently runs on Python 3.10.14. If a package is required which is not natively included in the python environment, you can install it using subprocess :

import subprocess
import sys

# Ensure the steamspypi package is installed
def install_package(package_name):
  try:
    __import__(package_name)
  except ImportError:
    print(f"{package_name} not found. Installing...")
    subprocess.check_call([sys.executable, "-m", "pip", "install", package_name])

install_package("steamspypi")

Replace steamspypi with the name of the package you want to install.

Code Node Output

Output from the code node is a JSON object with the following structure:

  "code_1": {
    "data": {
      "results": "Hello World from Function",
      "stdout": "Hello World from standard output\n",
      "stderr": "",
      "error": "",
      "sandbox_id": "sandbox-5067"
    },
    "error": null,
    "successfull": true,
    "successful": true
  }

Referencing Code Output

When selecting the output of a code node in a downstream node, you are given 3 options:

  1. code_node_name

  2. code_node_name.data.stdout

  3. code_node_name.data.results

Results

When you return an expression after a function, it can be referenced via {{code_block_name.data.results}}.

For example, the code:

def example():
   welcome = "Welcome to Orchestra"

   return welcome

example()

will output:

{
  "code_1": {
    "successfull": true,
    "data": {
      "results": "Welcome to Arcee Orchestra",
      "stdout": "",
      "stderr": "",
      "error": "",
      "sandbox_id": "sandbox-32ad"
    },
    "error": null
  }
}

{{code_1.data.results}} will return "Welcome to Arcee Orchestra".

Standard Output

When you print() an expression, it can be referenced via {{code_block_name.data.stdout}}.

For example, the code:

welcome = "Welcome to Orchestra"

print(welcome)

will output:

{
  "code_1": {
    "successfull": true,
    "data": {
      "results": "",
      "stdout": "Welcome to Orchestra\n",
      "stderr": "",
      "error": "",
      "sandbox_id": "sandbox-054a"
    },
    "error": null
  }
}

{{code_1.data.stdout}} will return "Welcome to Arcee Orchestra".

Code Node Name

Select the code node name for the output mapping when you want to reference a part of the code node output that is not results or stdout. For example, if you have a code node named code 1 and you wanted to check if there was an error in the code node, you could use:

{{code_1.data.error}}

Using Model Output in a Code Node

It's very common to take the output from a model node and pass it into a code node. This is often done to format the response, check for specific conditions or pass the response to custom integrations through APIs. The easiest way to do this is with the following:

model_output = f"""{{model_node}}"""

... the rest of your code ...

This is best when the model output is just text, for example, a summary or analysis. Model output needs to be handled differently if a specific output structure was instructed to the model, such as JSON.

Last updated