> For the complete documentation index, see [llms.txt](https://docs.arcee.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.arcee.ai/arcee-orchestra/workflows/workflow-components/code-node.md).

# 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` :&#x20;

```
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`&#x20;
3. `code_node_name.data.results`&#x20;

<details>

<summary>Results</summary>

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

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".

</details>

<details>

<summary>Standard Output</summary>

When you `print()` an expression, it can be referenced via `{{code_block_name.data.stdout}}`.&#x20;

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".

</details>

<details>

<summary>Code Node Name</summary>

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}}`

</details>

### 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 ...
```

{% hint style="info" %}
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.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.arcee.ai/arcee-orchestra/workflows/workflow-components/code-node.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
