BAML prompts are compiled into a messages array (or equivalent) that most LLM providers use:

BAML Prompt -> [{ role: "user": content: "hi there"}, { role: "assistant", ...}]

By default, BAML puts everything into a single message with the system role if available (or whichever one is best for the provider you have selected). When in doubt, the playground always shows you the current role for each message.

To specify a role explicitly, add the {{ _.role("user")}} syntax to the prompt

1prompt #"
2 {{ _.role("system") }} Everything after
3 this element will be a system prompt!
4
5 {{ _.role("user")}}
6 And everything after this
7 will be a user role
8"#

Try it out in PromptFiddle

BAML may change the default role to user if using specific APIs that only support user prompts, like when using prompts with images.

We use _ as the prefix of _.role() since we plan on adding more helpers here in the future.

Example — Using _.role() in for-loops

Here’s how you can inject a list of user/assistant messages and mark each as a user or assistant role:

BAML
1class Message {
2 role string
3 message string
4}
5
6function ChatWithAgent(input: Message[]) -> string {
7 client GPT4o
8 prompt #"
9 {% for m in messages %}
10 {{ _.role(m.role) }}
11 {{ m.message }}
12 {% endfor %}
13 "#
14}
BAML
1function ChatMessages(messages: string[]) -> string {
2 client GPT4o
3 prompt #"
4 {% for m in messages %}
5 {{ _.role("user" if loop.index % 2 == 1 else "assistant") }}
6 {{ m }}
7 {% endfor %}
8 "#
9}

Example — Using _.role() in a template string

BAML
1template_string YouAreA(name: string, job: string) #"
2 {{ _.role("system") }}
3 You are an expert {{ name }}. {{ job }}
4
5 {{ ctx.output_format }}
6 {{ _.role("user") }}
7"#
8
9function CheckJobPosting(post: string) -> bool {
10 client GPT4o
11 prompt #"
12 {{ YouAreA("hr admin", "You're role is to ensure every job posting is bias free.") }}
13
14 {{ post }}
15 "#
16}