Skip to main content

Python Code in SimplAI

In SimplAI, the Python code step allows users to process data dynamically using Python scripting. This step enables users to manipulate inputs, perform calculations, structure outputs, and integrate seamlessly with other workflow steps.

How It Works​

The Python code step operates within a predefined environment and interacts with other workflow steps through variables and structured data. Inputs: User-provided inputs and data from previous steps. Processing: The script executes based on user-defined logic. Output: The final processed result is returned and can be used in subsequent steps.

Accessing Variables​

Variables in SimplAI Python code can be accessed using double curly braces {{variable}}.

1. Accessing String Variables​

If a variable contains a string, enclose it in triple double quotes:

"""{{variable_name}}"""

2. Accessing Structured Data (JSON, Arrays, Objects, etc.)​

For structured data like JSON, arrays, or dictionaries, use the variable directly:

{{variable_name}}

3. Handling Multiple Outputs from a Variable​

If a variable contains multiple outputs, specify the exact output key:

{{variable.output_1}}
{{variable.output_2}}

4. Accessing Array Elements by Index​

If the variable is an array, access specific elements using an index:

{{variable.output_1.0}}  # Accessing the first element
{{variable.output_1.1}} # Accessing the second element

5. Accessing Foreach Outputs​

For workflows that use loops (foreach operations), access outputs using:

{{variable.foreach}}

Returning the Final Output​

To ensure the Python code step returns data properly for use in subsequent steps, always include a return statement.

1. Returning a Standard Variable​

return output  # If 'output' is stored as a variable within the Python code

2. Returning a String Variable​

return """{{variable}}"""

3. Returning a Structured Output (JSON, Array, etc.)​

return {{variable}}

Example Python Code in SimplAI​

# Access user inputs and previous step data
params = {{params}}
steps = {{steps}}

# Example: Extracting a user input string
user_message = """{{params.user_input}}"""

# Example: Processing structured data
structured_data = {{steps.previous_step.output_1}}
processed_output = structured_data.get("key", "Default Value")

# Returning the final output
return processed_output

Best Practices​

Always use proper variable formatting ("""{{variable}}""" for strings, {{variable}} for structured data). Include a return statement to make outputs available for subsequent steps. Use indexes ({{variable.0}}) when accessing array elements.