Code node
Execute any Python code
Code node
Code node allows you to execute any quick custom logic in a python environment. Open the code node and enter the code you want to run.
Code node executes the main
function from the code written in it.
The inputs to this node and output are determined based on the function signature.
For example, let’s consider signature: def main(string_list: list[str], chat_messages: list[ChatMessage]) -> str:
This signature will be parsed, and it will result in 2 inputs: string_list (StringList
) and chat_messages (ChatMessageList
). As a result, you’ll be able to connect 2 nodes, which have output types StringList
and ChatMessageList
, respectively.
Also, it will result in the fact that output type of this node will be String
. So you’ll be able to connect its output to any node, which expects input String
or Any
.
Supported types for both inputs and outputs:
- str
- list[str]
- list[ChatMessage]
- float (use float for int as well)
Whenever the signature is not parsing fails, check the following:
- The function name must be “main”.
- Annotate type of input arguments.
- Annotate return type of the function.
If one of the conditions is not met, the input/output handles won’t appear.
Below is the definition of ChatMessage.
Output type will be validated at runtime, and this node will fail if the returned output’s type doesn’t match the expected type.
Python version: 3.12.3
You can add other functions, if you need, and call them inside “main” function.
Do not remove main
function, as it is the entry point for the node execution.
Standard libraries are available. The “requests” library can also be used. External packages will be supported soon. Contact us to join the waitlist!
Available libraries
All standard libraries are available. For example, math
, random
, json
, etc.
You can import them as import math
and use them.
In addition, requests module is available for convenience of making API calls.