Documentation
HomeAPISign In
  • Getting Started
    • Overview
      • Core Concepts
      • Building your First Workflow
    • API Reference
  • Your Data
    • Brand Kits
    • Knowledge Bases
      • Add Data
        • Upload Files
        • Web Scrape
        • Import from Google Drive
        • Import from SQL Database
        • Import from Shopify
      • Knowledge Base Search
      • Knowledge Base Metadata
      • Knowledge Base API
  • Building Workflows
    • Workflow Concepts
      • Workflow Inputs
        • Input Types
      • Workflow Outputs
      • Variable Referencing
      • Liquid Templating
    • Workflow Steps
      • AI
        • Prompt LLM
          • Model Selection Guide
          • Prompting Guide
        • Transcribe Audio File
      • Web Research
        • Google Search
        • Web Page Scrape
      • Code
        • Run Code
        • Call API
        • Format JSON
        • Run SQL Query
        • Write Liquid Text
      • Flow
        • Condition
        • Iteration
        • Human Review
        • Content Comparison
        • Error
      • Data
        • Read from Grid
        • Write to Grid
        • Search Knowledge Base
        • Write to Knowledge Base
        • Get Knowledge Base File
      • AirOps
        • Workflow
        • Agent
      • Image & Video
        • Generate Image with API
        • Search Stock Images
        • Fetch Stock Image with ID
        • Resize Image
        • Screenshot from URL
        • Create OpenGraph Image
        • Create Video Avatar
      • SEO Research
        • Semrush
        • Data4SEO
      • Content Quality
        • Detect AI Content
        • Scan Content for Plagiarism
      • Content Processing
        • Convert Markdown to HTML
        • Convert PDF URL to Text
        • Group Keywords into Clusters
      • B2B Enrichment
        • Hunter.io
        • People Data Labs
      • CMS Integrations
        • Webflow
        • WordPress
        • Shopify
        • Contentful
        • Sanity
        • Strapi
      • Analytics Integrations
        • Google Search Console
      • Collaboration Integrations
        • Gmail
        • Google Docs
        • Google Sheets
        • Notion
        • Slack
    • Testing and Iteration
    • Publishing and Versioning
  • Running Workflows
    • Run Once
    • Run in Bulk (Grid)
    • Run via API
    • Run via Trigger
      • Incoming Webhook Trigger
      • Zapier
    • Run on a Schedule
    • Error Handling
  • Grids
    • Create a Grid
      • Import from Webflow
      • Import from Wordpress
      • Import from Semrush
      • Import from Google Search Console
    • Add Columns in the Grid
    • Run Workflows in the Grid
      • Add Workflow Column
      • Run Workflow Column
      • Map Workflow Outputs
      • Review Workflow Run Metadata
    • Review Content in the Grid
      • Review Markdown Content
      • Review HTML Content
      • Compare Content Difference
    • Publish to CMS from Grid
    • Pull Analytics in the Grid
    • Export as CSV
  • Copilot
    • Chat with Copilot
    • Edit Workflows with Copilot
    • Fix Errors with Copilot
  • Monitoring
    • Task Usage
    • Analytics
    • Alerts
    • Execution History
  • Your Workspace
    • Create a Workspace
    • Folders
    • Settings
    • Billing
    • Use your own LLM API Keys
    • Secrets
    • Team and Permissions
  • Chat Agents (Legacy)
    • Agent Quick Start
    • Chat Agents
    • Integrate Agents
      • Widget
      • Client Web SDK
  • About
    • Ethical AI and IP Production
    • Principles
    • Security and Compliance
Powered by GitBook
On this page
  • How does the Code Step work?
  • Javascript
  • How to Reference an Input
  • How to Reference an Output
  • Use a Return Statement
  • Javascript Example
  • Python
  • How to Reference an Input
  • How to Reference an Output
  • Use a Return Statement
  • Supported Python Packages
  • Adding Additional Python Packages
  • Python Example
  • AI Helpers
  • How to continue if the Code step fails
  • How to retry if the Code step fails
  • Limits

Was this helpful?

  1. Building Workflows
  2. Workflow Steps
  3. Code

Run Code

Add custom logic with Javascript or Python

Last updated 2 months ago

Was this helpful?

How does the Code Step work?

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.

Javascript

How to Reference an Input

Unlike Liquid, Javascript variables exclude {{ or }} . For example:

  • {{linkedin_url}} would be linkedin_url

How to Reference an Output

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

Use a Return Statement

You must include a return statement in a Code Step to determine the output of the step

Javascript Example

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:

Python

How to Reference an Input

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

How to Reference an Output

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

Use a Return Statement

You must include a return statement in a Code Step to determine the output of the step.

Supported Python Packages

As of 2/13/2024:

pandas==1.3.4
requests
beautifulsoup4
lxml
pyjwt
urllib3==1.26.6
scipy
numpy
pycryptodome
bing-image-urls
markdown
PyPDF2

Adding Additional Python Packages

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:

import os
import sys
import subprocess

# pip install custom package to /tmp/ and add to path
subprocess.call('pip install flask -t /tmp/ --no-cache-dir'.split(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
sys.path.insert(1, '/tmp/')
import flask

return 'import ok'

Note: Any external libraries called with this method will get installed every time the Code Step runs, potentially adding noticeable seconds of execution time.

Python Example

Let's walk through the same example from above, but in Python:

AI Helpers

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.

How to continue if the Code step fails

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

How to retry if the Code step fails

To retry a step if it fails,

  1. Select Continue instead of Terminate Workflow if the step fails

  2. Add a conditional where the condition checks if the error from the step exists e.g. step_1.error

  3. Add the step that you want to retry if there was an error

Limits

There are two main limits to be aware of regarding our Code Step:

  1. They have a maximum runtime of 15 minutes

  2. The maximum payload size is 6MB

Example Code Step
Click continue to continue the workflow
Check if an error occurred