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
  • loop
Prompt Syntax

Loops

Was this page helpful?
Edit this page
Previous

baml.cliPath

Next
Built with

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

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

loop

Jinja provides a loop object that can be used to access information about the loop. Here are some of the attributes of the loop object:

VariableDescription
loop.indexThe current iteration of the loop. (1 indexed)
loop.index0The current iteration of the loop. (0 indexed)
loop.revindexThe number of iterations from the end of the loop (1 indexed)
loop.revindex0The number of iterations from the end of the loop (0 indexed)
loop.firstTrue if first iteration.
loop.lastTrue if last iteration.
loop.lengthThe number of items in the sequence.
loop.cycleA helper function to cycle between a list of sequences. See the explanation below.
loop.depthIndicates how deep in a recursive loop the rendering currently is. Starts at level 1
loop.depth0Indicates how deep in a recursive loop the rendering currently is. Starts at level 0
loop.previtemThe item from the previous iteration of the loop. Undefined during the first iteration.
loop.nextitemThe item from the following iteration of the loop. Undefined during the last iteration.
loop.changed(*val)True if previously called with a different value (or not called at all).
1prompt #"
2 {% for item in items %}
3 {{ loop.index }}: {{ item }}
4 {% endfor %}
5"#