What is Jinja / Cookbook
What is Jinja / Cookbook
What is Jinja / Cookbook
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.
When in doubt — use the BAML VSCode Playground preview. It will show you the fully rendered prompt, even when it has complex logic.
{% ... %}: 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.Here’s how you can iterate over a list of items, accessing each item’s attributes:
Use conditional statements to control the flow and output of your templates based on conditions:
You can define and use variables within your templates to simplify expressions or manage data:
To promote reusability, you can include other templates within a template. See template strings:
BAML supports Python’s new-style string formatting via the .format() method on strings. This uses {} placeholders (not %s-style).
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.
See jinja docs