Agent Script Reference: List Variables

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 objects
3    all_accounts: mutable list[object]
4        description: "The list of account objects returned by the action"
5        label: "all_accounts"
6
7    # Declare a list of strings with default values
8    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.

OperationSyntax
Get the length of a listlen(@variables.myList)
Return the maximum of a list of numbersmax(@variables.scores)
Return the minimum of a list of numbersmin(@variables.scores)
Serialize a variable to a JSON stringto_json(@variables.myList)
Use JSONPath syntax to reference a specific variable in a listjson_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 questions
2    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```
7
8```sfdocs-code {"lang":"agentscript", "title": "Reference List Items in After Reasoning"}
9
10    # From a list of accounts, store the third and fourth account name in a variable
11    after_reasoning:
12
13        # stores Name field enclosed in [""], for example ["Pyramid Construction Inc."]
14        set @variables.third_account_name = json_path(@variables.all_accounts, "$[2].data.Name")
15
16        # stores Name field without added characters, for example Dickenson Inc
17        set @variables.fourth_account_name = @variables.all_accounts[3].data.Name

Related Topics