fern/03-reference/baml/prompt-syntax/role.mdx
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
prompt #"
{{ _.role("system") }} Everything after
this element will be a system prompt!
{{ _.role("user")}}
And everything after this
will be a user role
"#
Try it out in PromptFiddle
<Note> BAML may change the default role to `user` if using specific APIs that only support user prompts, like when using prompts with images. </Note>We use _ as the prefix of _.role() since we plan on adding more helpers here in the future.
_.role() in for-loopsHere's how you can inject a list of user/assistant messages and mark each as a user or assistant role:
class Message {
role string
message string
}
function ChatWithAgent(input: Message[]) -> string {
client GPT4o
prompt #"
{% for m in messages %}
{{ _.role(m.role) }}
{{ m.message }}
{% endfor %}
"#
}
function ChatMessages(messages: string[]) -> string {
client GPT4o
prompt #"
{% for m in messages %}
{{ _.role("user" if loop.index % 2 == 1 else "assistant") }}
{{ m }}
{% endfor %}
"#
}
_.role() in a template stringtemplate_string YouAreA(name: string, job: string) #"
{{ _.role("system") }}
You are an expert {{ name }}. {{ job }}
{{ ctx.output_format }}
{{ _.role("user") }}
"#
function CheckJobPosting(post: string) -> bool {
client GPT4o
prompt #"
{{ YouAreA("hr admin", "Your role is to ensure every job posting is bias free.") }}
{{ post }}
"#
}