ConfigsApr 1, 2026·2 min read

BAML — Type-Safe AI Function Framework

BAML adds engineering to prompt engineering with type-safe structured outputs. 7.8K+ stars. Python/TS/Ruby/Java, guaranteed schemas. Apache 2.0.

TL;DR
BAML gives you type-safe structured outputs from LLMs with guaranteed schema compliance.
§01

What it is

BAML (Basically A Made-up Language) is a framework that adds engineering rigor to prompt engineering. It provides type-safe structured outputs from LLMs with guaranteed schema compliance. You define the shape of your expected output as types, write your prompt in a .baml file, and BAML ensures the LLM response conforms to your schema. It supports Python, TypeScript, Ruby, and Java.

BAML targets AI application developers who need reliable structured data from LLMs, not free-form text. Whether you are extracting entities, classifying text, or generating structured records, BAML guarantees the output matches your type definition.

§02

Why it saves time or tokens

Parsing unstructured LLM output is fragile. JSON mode helps but still produces schema violations. BAML handles parsing, validation, and retry logic automatically. When the LLM returns malformed output, BAML fixes it or retries with a corrective prompt. This eliminates the custom parsing code and error handling that every AI application otherwise needs.

§03

How to use

  1. Install BAML: pip install baml-py or npm install @boundaryml/baml
  2. Define your types and functions in .baml files
  3. Generate client code and call the functions from your application
§04

Example

// classify.baml
class Sentiment {
  label string @description('positive, negative, or neutral')
  confidence float @description('0.0 to 1.0')
  reasoning string
}

function ClassifySentiment(text: string) -> Sentiment {
  client GPT4o
  prompt #"
    Analyze the sentiment of the following text.
    Text: {{ text }}
    {{ ctx.output_format }}
  "#
}
# Generated Python client
from baml_client import b

result = b.ClassifySentiment('This product is amazing')
print(result.label)       # 'positive'
print(result.confidence)  # 0.95
FeatureDescription
Type definitionsSchema-first output design
Code generationType-safe clients in 4 languages
Auto-retryFix malformed LLM responses
Multi-modelOpenAI, Anthropic, local models
StreamingStream partial structured results
§05

Related on TokRepo

§06

Common pitfalls

  • BAML's type system is separate from your language's types; keep the .baml definitions in sync with your application data models
  • Complex nested types with optional fields increase the chance of LLM parsing failures; keep schemas as flat as possible
  • The code generation step must run after every .baml file change; integrate it into your build pipeline to avoid stale clients

Frequently Asked Questions

How does BAML guarantee structured output?+

BAML generates a schema description in the prompt, instructing the LLM to output in a specific format. It then parses the response against the schema. If parsing fails, BAML can retry with a corrective prompt or attempt to fix common issues like missing fields or incorrect types automatically.

What LLM providers does BAML support?+

BAML supports OpenAI, Anthropic Claude, Google Gemini, Azure OpenAI, and local models through Ollama. You configure the provider in the .baml file as a client definition. Switching providers requires changing one line, not rewriting prompts.

How does BAML compare to Pydantic's AI features?+

Pydantic validates Python objects after you parse LLM output yourself. BAML handles the entire pipeline: prompt generation, output parsing, validation, and retry. BAML is language-agnostic (Python, TS, Ruby, Java) while Pydantic is Python-only. BAML's .baml files also provide a separation between prompt engineering and application code.

Does BAML support streaming?+

Yes. BAML supports streaming partial structured results. As the LLM generates output, BAML parses available fields and delivers partial objects. This lets your UI show results progressively rather than waiting for the complete response.

Is BAML open source?+

Yes. BAML is open source under the Apache 2.0 license. The core framework, code generators, and client libraries are all available on GitHub. There is no required hosted service; everything runs locally.

Citations (3)
🙏

Source & Thanks

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets