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
  • Jinja Cookbook
  • Basic Syntax
  • Loops / Iterating Over Lists
  • Conditional Statements
  • Setting Variables
  • Including other Templates
  • String Formatting
  • Built-in filters
Prompt Syntax

What is Jinja / Cookbook

Was this page helpful?
Edit this page
Previous

Jinja Filters

Next
Built with

BAML Prompt strings are essentially Minijinja templates, which offer the ability to express logic and data manipulation within strings. Jinja is a very popular and mature templating language amongst Python developers, so Github Copilot or another LLM can already help you write most of the logic you want.

Jinja Cookbook

When in doubt — use the BAML VSCode Playground preview. It will show you the fully rendered prompt, even when it has complex logic.

Basic Syntax

  • {% ... %}: Use for executing statements such as for-loops or conditionals.
  • {{ ... }}: Use for outputting expressions or variables.
  • {# ... #}: Use for comments within the template, which will not be rendered.

Loops / Iterating Over Lists

Here’s how you can iterate over a list of items, accessing each item’s attributes:

Jinja
1function MyFunc(messages: Message[]) -> string {
2 prompt #"
3 {% for message in messages %}
4 {{ message.user_name }}: {{ message.content }}
5 {% endfor %}
6 "#
7}

Conditional Statements

Use conditional statements to control the flow and output of your templates based on conditions:

Jinja
1function MyFunc(user: User) -> string {
2 prompt #"
3 {% if user.is_active %}
4 Welcome back, {{ user.name }}!
5 {% else %}
6 Please activate your account.
7 {% endif %}
8 "#
9}

Setting Variables

You can define and use variables within your templates to simplify expressions or manage data:

1function MyFunc(items: Item[]) -> string {
2 prompt #"
3 {% set total_price = 0 %}
4 {% for item in items %}
5 {% set total_price = total_price + item.price %}
6 {% endfor %}
7 Total price: {{ total_price }}
8 "#
9}

Including other Templates

To promote reusability, you can include other templates within a template. See template strings:

1template_string PrintUserInfo(arg1: string, arg2: User) #"
2 {{ arg1 }}
3 The user's name is: {{ arg2.name }}
4"#
5
6function MyFunc(arg1: string, user: User) -> string {
7 prompt #"
8 Here is the user info:
9 {{ PrintUserInfo(arg1, user) }}
10 "#
11}

String Formatting

BAML supports Python’s new-style string formatting via the .format() method on strings. This uses {} placeholders (not %s-style).

1{# Basic substitution #}
2{{ "{}, {}!".format("Hello", "World") }}
3{# Output: Hello, World! #}
4
5{# Number formatting with commas #}
6{{ "{:,}".format(1234567) }}
7{# Output: 1,234,567 #}
8
9{# Fixed decimal places #}
10{{ "{:.2f}".format(3.14159) }}
11{# Output: 3.14 #}
12
13{# Padding and alignment #}
14{{ "{:<10}".format("left") }}
15{{ "{:>10}".format("right") }}
16{{ "{:^10}".format("center") }}

Only new-style {} formatting is supported. Old-style %s formatting via the |format filter is not available because BAML uses the |format filter for serializing objects (e.g. value|format(type="yaml")).

For a full reference of what’s possible with Python’s format specification, see pyformat.info.

Built-in filters

See jinja docs