You can create a custom variable that is a list of values. You can make a list with any supported variable type, such as strings, booleans, numbers, or objects.
Creating a List Variable
When you create a list in the variables block, specify the variable type in brackets. You can leave the list uninitialized, initialize an empty list, or set default values.
Define List Variables
1variables:2 # Declare a list of objects3 all_accounts: mutable list[object]4 description: "The list of account objects returned by the action"5 label: "all_accounts"67 # Declare a list of strings with default values8 CompetencyQuestions: mutable list[string] = ["Tell me about a time you disagreed with a coworker.", "Tell me about one of your favorite shifts.", "What are the most important qualities in a candidate?"]9 description: "List of questions to determine competency."
Supported Operations on List Variables
You can use these operations on your list variables.
Operation
Syntax
Get the length of a list
len(@variables.myList)
Return the maximum of a list of numbers
max(@variables.scores)
Return the minimum of a list of numbers
min(@variables.scores)
Serialize a variable to a JSON string
to_json(@variables.myList)
Use JSONPath syntax to reference a specific variable in a list
json_path(@variables.myList, "$[*].name")
Objects in a List
Each item in a list[object] is a record wrapped in a container. Access record fields through .data.<FieldName> — for example, .data.Name rather than .Name. Use JSONPath’s $[<index>].data.<FieldName> for the equivalent selector.
Reference Items in a List
To reference an item in a list, use json_path or [] notation. Lists are zero-indexed. To get the first item in your list, use [0].
JSONPath returns the value enclosed in [""] characters, for example [“Pyramid Construction Inc.”].
[] notation returns just the value, without enclosing json characters
Reference a List Item in Reasoning Instructions
1# Ask the user the 3rd question in the list of questions2 reasoning:3 # If the 3rd question is "what is your address?", the prompt resolves to "Ask the user this question: What is your address?".4 instructions: ->5 | Ask the user this question: {!@variables.CompetencyQuestions[2]}6```78```sfdocs-code {"lang":"agentscript", "title": "Reference List Items in After Reasoning"}910 # From a list of accounts, store the third and fourth account name in a variable11 after_reasoning:1213 # stores Name field enclosed in [""], for example ["Pyramid Construction Inc."]14 set @variables.third_account_name = json_path(@variables.all_accounts, "$[2].data.Name")1516 # stores Name field without added characters, for example Dickenson Inc17 set @variables.fourth_account_name = @variables.all_accounts[3].data.Name