# BAML

> A prompting language that turns LLM prompts into typed functions with reliable structured output

- Category: App Orchestration / Structured Output
- Language: Rust
- Repository: https://github.com/BoundaryML/baml
- Website: https://boundaryml.com/
- Canonical page: https://ai-tldr.dev/tools/baml/

## In short

BAML ("Basically a Made-up Language") is a small domain-specific language for writing LLM prompts. Instead of building free-form strings, you define each prompt as a function with typed inputs and a typed return value. It sits in the Structured Output category of the App Orchestration tools and is open-source, written in Rust.

## Overview

BAML ("Basically a Made-up Language") is a small domain-specific language for writing LLM prompts. Instead of building free-form strings, you define each prompt as a function with typed inputs and a typed return value. Its Rust compiler then generates a client library (the baml_client) that you call from your own code, so model output comes back as structured, type-safe data rather than raw text you have to parse by hand.

It is aimed at developers building AI workflows and agents who want predictable, structured output across different models. You only write the prompts in BAML; the rest of your application stays in the language you already use. BAML provides quickstarts for Python, TypeScript, Ruby, and Go, plus a REST API path for other languages.

Within the structured-output and LLM-orchestration space, BAML focuses on schema engineering. It includes streaming, retries, fallbacks, and model rotations, and its schema-aligned parsing (SAP) approach handles flexible model responses even when a model does not support native tool-calling APIs.

## What it does

- Define prompts as functions with typed parameters and return types, compiled into a generated baml_client
- Call the same BAML functions from Python, TypeScript, Ruby, Go, or any language via a REST API
- Schema-aligned parsing (SAP) for reliable structured output and tool-calling even with models that lack native tool-call APIs
- Type-safe streaming, including partial results during the stream and a final typed response
- Switch between many models by changing the client line, with retry policies, fallbacks, and round-robin model rotations
- IDE tooling for VS Code and JetBrains to visualize prompts, inspect the raw API request, and run tests in the playground

## Getting started

Install the BAML package for your language, initialize a project, write a function in a .baml file, then generate the client and call it from your code. The example below uses Python.

1. Install and initialize

   Install the Python package and scaffold a BAML project. This creates a baml_src directory for your .baml files.

```bash
pip install baml-py
baml-cli init
```

2. Define a function in BAML

   In your .baml file, declare a function with typed inputs and a typed return value, the model it uses, and its prompt.

```rust
function ChatAgent(message: Message[], tone: "happy" | "sad") -> StopTool | ReplyTool {
    client "openai/gpt-4o-mini"

    prompt #"
        Be a {{ tone }} bot.

        {{ ctx.output_format }}

        {% for m in message %}
        {{ _.role(m.role) }}
        {{ m.content }}
        {% endfor %}
    "#
}
```

3. Generate the client

   Run generate to produce the baml_client code that lets you call your BAML functions. With the VS Code extension installed, this also runs automatically on save.

```bash
baml-cli generate
```

4. Call it from your code

   Import the generated client and call your function. The return value is typed, so you can branch on the result.

```python
from baml_client import b
from baml_client.types import Message, StopTool

messages = [Message(role="assistant", content="How can I help?")]
tool = b.ChatAgent(messages, "happy")
if isinstance(tool, StopTool):
    print("Goodbye!")
```

## When to use it

- Extracting structured data, such as fields from a resume or document, into typed objects instead of parsing raw JSON yourself
- Building chat agents and multi-step workflows as while loops that call chained BAML functions with shared state
- Getting reliable tool-calling and structured output from models that do not support native tool-call APIs
- Adding retries, fallbacks, and model rotations across many providers without changing your application code
