Language Fundamentals
File Structure
BAML code is organized into.baml files within your baml_src directory. Each file can contain:
- Functions: AI function definitions with prompts and return types
- Classes: Structured data types for inputs and outputs
- Enums: Enumerated values for classification tasks
- Clients: LLM client configurations
- Tests: Test cases for your AI functions
- Template Strings: Reusable prompt templates
- Retry Policies: Retry strategies for client requests
Comments
Standard single-line comments that are ignored during compilation.
Documentation comments that are carried through to generated code.
Multi-line comments within block strings.
String Syntax
Quoted Strings
Standard double-quoted strings for inline values:Quoted strings cannot contain template variables. Use block strings for templating.
Unquoted Strings
Simple strings without quotes for configuration values:Block Strings
Multi-line strings with automatic dedentation and Jinja templating support:Escape Sequences
To include"# within a block string, use different hash counts:
Type System
BAML supports a rich type system for defining inputs and outputs:| Type | Example | Description |
|---|---|---|
string | name string | Text data |
int | age int | Integer numbers |
float | score float | Decimal numbers |
bool | isActive bool | Boolean values |
null | value null | Null value |
enum | status Status | User-defined enums |
class | user User | User-defined classes |
image | photo image | Image media |
audio | clip audio | Audio media |
video | recording video | Video media |
pdf | document pdf | PDF documents |
Type? | age int? | Optional type |
Type[] | tags string[] | Array/list |
Type1 | Type2 | int | string | Union types |
map<K, V> | map<string, int> | Key-value maps |
| Literals | "red" | "blue" | Literal values |
Type Aliases
Define reusable type aliases:Core Language Elements
Function
Define AI functions with typed inputs and outputs:Class
Define structured data types:Enum
Define enumerated values:Client
Configure LLM clients:Test
Define test cases:Naming Conventions
| Element | Convention | Example |
|---|---|---|
| Functions | PascalCase (must start with capital) | ExtractData |
| Classes | PascalCase | UserProfile |
| Enums | PascalCase | Status |
| Properties | snake_case or camelCase | user_name, userName |
| Enum values | PascalCase | Active, Inactive |
| Clients | PascalCase | GPT4Turbo |
Environment Variables
Access environment variables using theenv. prefix:
Jinja Templating
BAML uses Jinja syntax for dynamic prompt generation:Special Variables
| Variable | Description |
|---|---|
ctx.output_format | Auto-generated format instructions |
ctx.client | Current client and model name |
_.role(name) | Set message role for chat models |
Best Practices
- Type Safety: Always use explicit types for function parameters and return values
- Reusability: Use template strings for common prompt patterns
- Documentation: Add docstrings to classes and properties for better generated code
- Validation: Use tests to validate AI function behavior
- Error Handling: Leverage optional types (
?) for fields that may not always be present - Composition: Prefer composition over complex nested structures
Unsupported Features
any/jsontype - Use specific types or dynamic types insteaddatetime- Usestringwith description specifying formatduration- Usestringwith ISO8601 format descriptionSet- Use arrays ([]) insteadTuple- Use classes instead- Class inheritance - Use composition pattern
Next Steps
Functions
Define AI functions with prompts and types
Classes
Create structured data types
Enums
Define classification values
Clients
Configure LLM providers