Map values (AKA Dictionaries) allow you to store key-value pairs.

Most of BAML (clients, tests, classes, etc) is represented as a map.

Syntax

To declare a map in a BAML file, you can use the following syntax:

1{
2 key1 value1,
3 key2 {
4 nestedKey1 nestedValue1,
5 nestedKey2 nestedValue2
6 }
7}

Key Points:

  • Colons: Not used in BAML maps; keys and values are separated by spaces.
  • Value Types: Maps can contain unquoted or quoted strings, booleans, numbers, and nested maps as values.
  • Classes: Classes in BAML are represented as maps with keys and values.

Usage Examples

Example 1: Simple Map

1class Person {
2 name string
3 age int
4 isEmployed bool
5}
6
7function DescribePerson(person: Person) -> string {
8 client "openai/gpt-4o-mini"
9 prompt #"
10 Describe the person with the following details: {{ person }}.
11 "#
12}
13
14test PersonDescription {
15 functions [DescribePerson]
16 args {
17 person {
18 name "John Doe",
19 age 30,
20 isEmployed true
21 }
22 }
23}

Example 2: Nested Map

1class Company {
2 name string
3 location map<string, string>
4 employeeCount int
5}
6
7function DescribeCompany(company: Company) -> string {
8 client "openai/gpt-4o-mini"
9 prompt #"
10 Describe the company with the following details: {{ company }}.
11 "#
12}
13
14test CompanyDescription {
15 functions [DescribeCompany]
16 args {
17 company {
18 name "TechCorp",
19 location {
20 city "San Francisco",
21 state "California"
22 },
23 employeeCount 500
24 }
25 }
26}

Example 3: Map with Multiline String

1class Project {
2 title string
3 description string
4}
5
6function DescribeProject(project: Project) -> string {
7 client "openai/gpt-4o-mini"
8 prompt #"
9 Describe the project with the following details: {{ project }}.
10 "#
11}
12
13test ProjectDescription {
14 functions [DescribeProject]
15 args {
16 project {
17 title "AI Research",
18 description #"
19 This project focuses on developing
20 advanced AI algorithms to improve
21 machine learning capabilities.
22 "#
23 }
24 }
25}