Variables
Variables in BAML prompts allow you to inject dynamic content.
Basic Variable Output
Use double curly braces to output variable values:
{{ variable_name }}
{{ user.name }}
{{ items[0] }}
Function Parameters
Function parameters are automatically available as variables:
function Greet(name: string, age: int) -> string {
client "openai/gpt-4o"
prompt #"
Hello {{ name }}, you are {{ age }} years old.
"#
}
Accessing Object Properties
Access nested properties using dot notation:
class User {
name string
profile Profile
}
class Profile {
bio string
}
function DescribeUser(user: User) -> string {
client "openai/gpt-4o"
prompt #"
Name: {{ user.name }}
Bio: {{ user.profile.bio }}
"#
}
Array Access
Access array elements by index:
{{ items[0] }}
{{ messages[1].content }}
Setting Variables
Define local variables using {% set %}:
{% set greeting = "Hello" %}
{{ greeting }}, {{ name }}!
{% set total = 0 %}
{% for item in items %}
{% set total = total + item.price %}
{% endfor %}
Total: {{ total }}
Default Values
Provide default values for optional or undefined variables:
{{ user_name|default("Guest") }}
{{ pi or 3.14159 }}