Skip to main content

Maps

Maps (dictionaries) store key-value pairs.

Syntax​

map<KeyType, ValueType>

Supported Key Types​

  • string
  • enum values
  • Literal strings ("A" | "B" | "C")

Examples​

Basic Map​

map<string, string>
map<string, int>
map<string, MyClass>

Enum Keys​

enum Category {
A
B
C
}

map<Category, string>

Literal String Keys​

map<"A" | "B" | "C", string>

Usage Examples​

Simple Map​

class Person {
name string
age int
isEmployed bool
}

function DescribePerson(person: Person) -> string {
client "openai/gpt-4o-mini"
prompt #"
Describe the person: {{ person }}.
"#
}

test PersonDescription {
functions [DescribePerson]
args {
person {
name "John Doe",
age 30,
isEmployed true
}
}
}

Nested Map​

class Company {
name string
location map<string, string>
employeeCount int
}

function DescribeCompany(company: Company) -> string {
client "openai/gpt-4o-mini"
prompt #"
Describe the company: {{ company }}.
"#
}

test CompanyDescription {
functions [DescribeCompany]
args {
company {
name "TechCorp",
location {
city "San Francisco",
state "California"
},
employeeCount 500
}
}
}

Map with Multiline String​

class Project {
title string
description string
}

test ProjectDescription {
functions [DescribeProject]
args {
project {
title "AI Research",
description #"
This project focuses on developing
advanced AI algorithms to improve
machine learning capabilities.
"#
}
}
}

Key Points​

  • Keys and values are separated by spaces (no colons)
  • Maps can contain unquoted or quoted strings, booleans, numbers, and nested maps
  • Classes in BAML are represented as maps with keys and values
  • Most of BAML (clients, tests, classes, etc.) is represented as a map

See Also​