For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Help on Discord
HomeGuideExamplesBAML ReferencePlaygroundAgents.mdChangelog
HomeGuideExamplesBAML ReferencePlaygroundAgents.mdChangelog
    • Overview
  • baml-cli
    • init
    • generate
    • test
    • serve
    • dev
    • fmt
  • Language Reference
    • Types
    • function
    • test
    • template_string
    • client<llm>
    • class
    • enum
    • generator
  • Generated baml_client
    • with_options(..)
    • AbortSignal / Cancellation
    • Collector
    • logging / env vars
    • AsyncClient / SyncClient
    • TypeBuilder
    • ClientRegistry
    • client Option
    • OnTick
    • Multimodal
    • Image
    • Audio
    • Pdf
    • Video
  • Attributes
    • What are attributes?
    • @alias / @@alias
    • @description / @@description
    • @skip
    • @assert
    • @check
    • Jinja in Attributes
    • @@dynamic
  • LLM Client Providers
    • Overview
    • AWS Bedrock
    • Anthropic
    • Google AI: Gemini
    • Google: Vertex
    • OpenAI
    • OpenAI Responses API
    • OpenAI from Azure
    • OpenRouter
    • openai-generic
    • Microsoft Foundry (openai-generic)
    • Cerebras (openai-generic)
    • Groq (openai-generic)
    • Hugging Face (openai-generic)
    • Keywords AI (openai-generic)
    • Llama API (openai-generic)
    • Litellm (openai-generic)
    • LM Studio (openai-generic)
    • Ollama (openai-generic)
    • Vercel AI Gateway (openai-generic)
    • Tinfoil (openai-generic)
    • TogetherAI (openai-generic)
    • Unify AI (openai-generic)
    • vLLM (openai-generic)
  • LLM Client Strategies
    • Timeout Configuration
    • Retry Policy
    • Fallback
    • Round Robin
  • Prompt Syntax
    • What is jinja?
    • Jinja Filters
    • ctx.output_format
    • ctx.client
    • _.role
    • Variables
    • Conditionals
    • Loops
  • Editor Extension Settings
    • baml.cliPath
    • baml.generateCodeOnSave
    • baml.enablePlaygroundProxy
    • baml.syncExtensionToGeneratorVersion
Help on Discord
LogoLogo
On this page
  • Example — Using _.role() in for-loops
  • Example — Using _.role() in a template string
Prompt Syntax

_.role

Was this page helpful?
Edit this page
Previous

Variables

Next
Built with

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", "Your role is to ensure every job posting is bias free.") }}
13
14 {{ post }}
15 "#
16}