Variables
Learn how to properly leverage variables in your AI Workflows
Variables in MindStudio are dynamic placeholders that store data during workflow execution. They allow you to pass information between blocks and workflows seamlessly.
Example:
Variable Name:
userName
Usage:
"Hello,
{{userName}}
! Welcome to our app."
Creating Variables
Variables are created automatically in MindStudio whenever:
A User Input collects data.
A block generates an output (e.g., Generate Text Block, Analyze Image Block).
You manually define them in the Start Block.
Types of Variables
Launch Variables
These are defined in the Start Block of your workflow. Values for these variables are passed in as arguments when a workflow is run via API or via the Run Workflow block.
Runtime Variables
Some blocks, such as Generate Text Blocks or User Input Blocks, assign values for the variable while the workflow is running. For Example, after performing a Google Search, the block can store the results in a variable called google_result
.
Calling Variables
To use a variable in any block or prompt, reference it by enclosing the variable name in double curly braces: {{variable_name}}
.
Example Calling Variables in a Generate Text Block:
Extracting a Value from a JSON Structure
MindStudio provides tools for extracting specific values from JSON objects using the JSON Path syntax and the get
helper. This allows workflows to handle and manipulate structured data with precision, making them more dynamic and adaptable.
get
Helper - Query JSON Variables
get
Helper - Query JSON VariablesThe get
helper allows you to query JSON variables for specific values using JSON Path expressions. This feature is especially useful when working with nested or complex JSON structures.
Syntax:
Example 1: Extract a Nested Value:
Given the following JSON assigned to myJsonVariable
:
Use this to extract the email address:
Output: alice@example.com
Example 2: Extract the First Item in an Array:
Given the following JSON:
Use this to extract the name of the first item:
Output: Foo
Example 3: Extract Multiple Values:
JSON Path also allows for querying multiple elements. Given the following JSON:
Output: ["Foo", "Bar"]
Best Practices When Extracting JSON
Validate JSON Structure: Ensure your variable contains valid JSON data before attempting to extract values.
Handle Missing Values: Include fallback logic in your workflow to handle cases where the expected path does not exist in the JSON.
Using Handlebars Templating
Conditional Logic
Handlebars supports if-else
logic for dynamic outputs:
Special Handlebars Methods in MindStudio
In addition to standard Handlebars features, MindStudio introduces special methods for advanced functionality:
{{json varName}}
{{json varName}}
Converts a JSON object into a string format.
Example:
If userProfile
contains:
Usage:
Output:
{{sample varName number token}}
{{sample varName number token}}
Extracts a portion of the variable's content based on specified parameters.
Parameters:
varName: The variable to sample.
number: The number of items (e.g., lines, words, or letters). If negative, starts from the end.
token: The type of unit to extract (
line
,word
, orletter
).
Examples:
Extract the first 5 words:
Extract the last 3 lines:
Extract the first 10 letters:
{{#if condition}}
{{#if condition}}
Converts a conditional block that renders content only when the condition is true.
Example:
If isLoggedIn
is true
Usage:
Output:
{{#unless condition}}
{{#unless condition}}
Converts a conditional block that renders content only when the condition is false.
Example:
If isLoggedIn
is false
Usage:
Output:
{{#each array}}
{{#each array}}
Iterates over an array or object and renders the block for every item.
Example:
If items
contains:
Usage:
Output:
{{#with object}}
{{#with object}}
Changes the evaluation context to the provided object for the enclosed block.
Example:
If user
contains:
Usage:
Output:
{{lookup object key}}
{{lookup object key}}
Dynamically looks up a property from an object using a key.
Example:
If user
contains:
Usage:
Output:
{{log value}}
{{log value}}
Logs a value to the console for debugging purposes.
Example:
If debugData
contains:
Usage:
Output:
(Check the browser console for the logged value)
{{get varName "property"}}
{{get varName "property"}}
Retrieves a nested property using a JSONPath expression from a JSON object.
Example:
If userData
contains:
Usage:
Output:
{{add num increment}}
{{add num increment}}
Adds a numeric increment to a given number.
Example:
If num
is:
Usage:
Output:
{{subtract num decrement}}
{{subtract num decrement}}
Subtracts a numeric value from a given number.
Example:
If num
is:
Usage:
Output:
{{multiply value multiplier}}
{{multiply value multiplier}}
Multiplies two numbers.
Example:
If value
is:
Usage:
Output:
{{divide dividend divisor}}
{{divide dividend divisor}}
Divides one number by another.
Example:
If dividend
is:
Usage:
Output:
{{eq var1 var2}}
{{eq var1 var2}}
Checks if two values are equal using the double-equals operator.
Example:
If var1
contains:
and var2
contains:
Usage:
Output:
{{gt value1 value2}}
{{gt value1 value2}}
Checks if the first value is greater than the second value.
Example:
If value1
is:
and value2
is:
Usage:
Output:
{{gte value1 value2}}
{{gte value1 value2}}
Checks if the first value is greater than or equal to the second value.
Example:
If value1
is:
and value2 is:
Usage:
Output:
{{lt value1 value2}}
{{lt value1 value2}}
Checks if the first value is less than the second value.
Example:
If value1
is:
and value2
is:
Usage:
Output:
{{lte value1 value2}}
{{lte value1 value2}}
Checks if the first value is less than or equal to the second value.
Example:
If value1
is:
and value2
is:
Usage:
Output:
{{isEmpty varName}}
{{isEmpty varName}}
Evaluates whether a variable is empty (null, undefined, an empty string, an empty array, or an empty object).
Example:
If data
contains:
Usage:
Output:
{{length varName}}
{{length varName}}
Returns the length of an array or a string. Returns "NaN" if the variable is not an array or string.
Example:
If list
contains:
Usage:
Output:
{{markdown varName}}
{{markdown varName}}
Converts a Markdown-formatted string into HTML.
Example:
If markdownText
contains:
Usage:
Output:
{{formattedNumber number fractionDigits}}
{{formattedNumber number fractionDigits}}
Formats a number using locale‑specific formatting with a fixed number of fractional digits.
Example:
If amount
is:
Usage:
Output:
{{abbreviatedNumber number fractionDigits}}
{{abbreviatedNumber number fractionDigits}}
Formats a number into a compact, abbreviated notation (e.g., 1K, 1M) with a specified number of fractional digits.
Example:
If amount
is:
Usage:
Output:
{{date varName format}}
{{date varName format}}
Formats a date string according to the specified format. Supports both custom formats (e.g., "YYYY-MM-DD") and relative keywords like "fromNow" or "toNow".
Example:
If dateString
contains:
Usage:
Output:
Last updated