Functions in BAML define the contract between your application and AI models, providing type-safe interfaces for AI operations.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/BoundaryML/baml/llms.txt
Use this file to discover all available pages before exploring further.
Overview
A BAML function consists of:- Input parameters with explicit types
- A return type specification
- An LLM client reference
- A prompt template (as a block string)
Syntax
Components
The
function keyword begins the declaration.Function name must start with a capital letter and follow PascalCase convention.
One or more typed parameters, comma-separated.
Arrow operator separates parameters from return type.
The type that the function guarantees to return. Can be any BAML type including unions.
Specifies which LLM client to use. Can be a client name or inline provider/model.
The prompt template using Jinja syntax. Must be a block string (
#"..."#).Type System
Functions leverage BAML’s strong type system for both inputs and outputs.Primitive Types
| Type | Description | Example |
|---|---|---|
string | Text data | text: string |
int | Integer numbers | count: int |
float | Decimal numbers | score: float |
bool | Boolean values | isValid: bool |
Complex Types
| Type | Description | Example |
|---|---|---|
Class | Custom classes | person: Person |
Enum | Enumerations | status: Status |
Type[] | Arrays | items: string[] |
Type? | Optional | age: int? |
Type1 | Type2 | Unions | -> string | Error |
map<K,V> | Maps | data: map<string, int> |
Multimodal Types
| Type | Description | Example |
|---|---|---|
image | Image inputs | photo: image |
audio | Audio inputs | recording: audio |
video | Video inputs | clip: video |
pdf | PDF documents | doc: pdf |
Examples
Basic Function
Multiple Parameters
Complex Return Types
Union Return Types
Multimodal Input
Literal Return Types
Prompt Templates
Jinja Syntax
BAML uses Jinja for dynamic prompt generation:Special Variables
ctx.output_format
Automatically generates format instructions based on the function’s return type.
ctx.client
Contains the selected client and model name.
_.role(role_name)
Defines the role for the message chunk in chat-based models.
Client Specification
Named Client
Reference a client defined elsewhere:Inline Client
Use provider/model shorthand:Generated Code Integration
Functions generate type-safe code in your target language:Error Handling
Functions automatically handle common AI model errors:- JSON parsing errors: Automatically corrected when possible
- Type mismatches: Detected and reported with detailed error messages
- Network errors: Propagated to caller with retry support (via client retry policies)
- Rate limits: Handled according to client retry policy
Runtime Client Selection
To select which client to use at runtime, use the client registry:- A/B testing different models
- Gradual rollouts
- Fallback scenarios
- Cost optimization
Best Practices
- Naming: Use descriptive PascalCase names starting with capital letters
- Parameters: Keep parameter lists focused and well-typed
- Return Types: Use specific types rather than unions when possible
- Prompts: Include
{{ ctx.output_format }}for structured outputs - Documentation: Add docstrings to explain function purpose
- Testing: Create tests for each function variation
- Client Selection: Use named clients for reusability