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
  • Basic Usage
  • BAML Custom Filters
  • format
  • regex_match
  • sum
  • Standard Jinja Filters
  • length
  • upper / lower
  • title
  • trim
  • default
  • join
  • first / last
  • sort
  • unique
  • reverse
  • replace
  • round
  • abs
  • Chaining Filters
  • More Information
Prompt Syntax

Jinja Filters

Was this page helpful?
Edit this page
Previous

ctx.output_format

Next
Built with

Jinja filters allow you to transform and format values within your BAML prompts. Filters are applied using the pipe operator (|).

Basic Usage

1{{ value|filter_name }}
2{{ value|filter_name(arg1, arg2) }}

BAML Custom Filters

format

Serializes values into structured text while preserving all BAML-specific aliases. Use the required type argument to choose the output representation.

This is BAML’s serialization filter, not Python’s %-style string formatting. The |format filter in BAML always requires a type keyword argument.

For string formatting, use Python’s .format() method syntax instead: "{}, {}!".format("Hello", "World"). See the String Formatting cookbook for examples.

Signature: value|format(type="yaml" | "json" | "toon", **kwargs)

Required arguments:

  • type (string): "yaml", "json", or "toon" (alias for TOON serializer)

Format-specific behavior:

FormatDescriptionExtra kwargs
yamlHuman-friendly YAML string with enum/class aliasesNone
jsonStandard JSON string (minified) with alias-safe serializationNone
toonCompact TOON format for uniform arraysindent, delimiter, length_marker

All formats reuse the same alias-aware serializer, so enums emit their @alias values and class properties honor @alias names at every nesting level (lists, maps, nested classes, etc.).

Examples:

1{# YAML for readability #}
2{{ organization|format(type="yaml") }}
3
4{# JSON for API interop #}
5{{ organization|format(type="json") }}
6
7{# TOON with custom options #}
8{{ organization|format(type="toon", indent=4, delimiter="tab") }}

TOON options (only when format="toon"):

  • indent (int, default 2): spaces per indent level
  • delimiter ("comma" | "tab" | "pipe", default "comma"): column separator for arrays
  • length_marker (single char, optional): prefix to annotate array lengths

Invalid combinations raise a template error (e.g., missing type or an unknown delimiter for TOON).

About TOON:

TOON (Token-Oriented Object Notation) combines YAML’s indentation with CSV-style tabular layout. It’s designed for uniform arrays of objects (multiple fields per row, same structure across items), where it can provide significant token savings while maintaining LLM-friendly structure validation through explicit array lengths and field headers.

When TOON works best:

  • Uniform arrays of objects (e.g., lists of users, transactions, products)
  • Highly structured, tabular data

When other formats may be better:

  • Deeply nested structures (compact JSON may use fewer tokens)
  • Semi-uniform or mixed data
  • Pure flat tables (CSV is more compact)

For benchmarks, detailed comparisons, and the full specification, see the TOON repository.

regex_match

Tests if a string matches a regular expression pattern.

Signature: value|regex_match(pattern)

Example:

1{% if phone_number|regex_match("\\(?\\d{3}\\)?[-.\\s]?\\d{3}[-.\\s]?\\d{4}") %}
2 Valid phone number
3{% endif %}

sum

Sums all numeric values in an array.

Signature: array|sum

Example:

1Total: {{ prices|sum }}

Standard Jinja Filters

BAML supports all standard Minijinja filters. Here are some commonly used ones:

length

Returns the length of a string, array, or object.

1{{ items|length }} items

upper / lower

Converts string to uppercase or lowercase.

1{{ name|upper }}
2{{ name|lower }}

title

Converts string to title case.

1{{ title|title }}

trim

Removes whitespace from both ends of a string.

1{{ text|trim }}

default

Provides a default value if the variable is undefined or empty.

1{{ user_name|default("Guest") }}

join

Joins array elements into a string with a separator.

1{{ tags|join(", ") }}

first / last

Returns the first or last element of an array.

1{{ items|first }}
2{{ items|last }}

sort

Sorts an array.

1{% for item in items|sort %}
2 {{ item }}
3{% endfor %}

unique

Removes duplicate values from an array.

1{{ values|unique }}

reverse

Reverses an array or string.

1{{ items|reverse }}

replace

Replaces occurrences of a substring.

1{{ text|replace("old", "new") }}

round

Rounds a number to a specified precision.

1{{ price|round(2) }}

abs

Returns the absolute value of a number.

1{{ value|abs }}

Chaining Filters

You can chain multiple filters together:

1{{ name|trim|lower|title }}
2{{ items|unique|sort|join(", ") }}

More Information

For a complete list of standard Jinja filters, see the Minijinja documentation.