Code
Add custom logic with Javascript or Python
Last updated
Add custom logic with Javascript or Python
Last updated
The Code Step in AirOps Studio currently supports Javascript and Python, which provides you with the flexibility needed to perform advanced requests.
The code in a Code Step is wrapped inside a function, and you must use a return statement to output results from the step.
Unlike Liquid, Javascript variables exclude {{
or }}
. For example:
{{linkedin_url}}
would be linkedin_url
Javascript syntax is similar to Liquid, but excludes brackets:
{{step_1.output}}
would be step_1.output
{{step_1.output.name}}
would be step_1.output.name
You must include a return
statement in a Code Step to determine the output of the step
Let's walk through an example that references both a text input as well as an LLM output. We'll use a basic Workflow that requests a string input, and returns a match of words that rhyme with it.
Our Code Step will simply be responsible for combining the input prompt with the LLM output, so we have just a single result that includes the question and answer:
Similar to Javascript, Python only requires direct reference to the variable name. So the same example from above holds true:
{{linkedin_url}}
would be linkedin_url
Python syntax must include brackets around the variable name:
{{step_1.output}}
would be step_1["output"]
{{step_1.output.key}}
would be step_1["output"]["key"]
You must include a return
statement in a Code Step to determine the output of the step.
As of 2/13/2024:
While we're always looking to add to our natively supported packages, our Code Step does offer a workaround for working with additional packages.
When writing your Code Step, append the following code to your block:
Note: Any external libraries called with this method will get installed every time the Code Step runs, potentially adding noticeable seconds of execution time.
Let's walk through the same example from above, but in Python:
Both Python and Javascript support using our AI Helper to generate code. This can be useful if you're struggling to find the exact syntax in either language to achieve your goal.
Similar to the prompt for an LLM, we recommend being as clear and thorough in your description as possible.
Once our AI Helpers have generated the code, you can keep or reject the generated text. If you decide you want to add or remove the code later on, you can use the same "Generate with AI" button to modify your prompt and code.
By default, the code step will terminate the workflow if it fails. However, to continue the workflow if the step fails, simply click Continue
at the bottom of the step.
The step will return the following keys:
output
: this will be null
error
:
message
: the message returned from the step
code
: the error code representing the error
To retry a step if it fails,
Select Continue
instead of Terminate Workflow
if the step fails
Add a conditional where the condition checks if the error
from the step exists e.g. step_1.error
Add the step that you want to retry if there was an error
There are two main limits to be aware of regarding our Code Step:
They have a maximum runtime of 15 minutes
The maximum payload size is 6MB