The BAML Type System
BAML's type system is the foundation for reliable AI applications. It ensures LLM outputs are always valid and usable.
Core Types
Primitives
| Type | Description | Example |
|---|---|---|
string | Text | "hello" |
int | Integer | 42 |
float | Decimal | 3.14 |
bool | Boolean | true |
Complex Types
// Enums - constrained choices
enum Status {
pending
approved
rejected
}
// Classes - structured objects
class User {
name string
email string
age int? // Optional field
}
// Arrays
class Response {
items string[]
users User[]
}
Type Safety at Runtime
Unlike traditional type systems that only work at compile time, BAML validates types at runtime when the LLM responds:
LLM Output → BAML Parser → Validated Type → Your Code
This is called Schema-Aligned Parsing.
Why This Matters
- No more JSON parsing errors - BAML handles malformed outputs
- Guaranteed structure - Your code always receives the expected type
- Provider independence - Same types work across OpenAI, Anthropic, etc.
Learn More
- Schema-Aligned Parsing - How BAML parses and validates outputs
- Classification Example - See types in action
- BAML Syntax Reference - Complete type syntax