Skip to main content

Installation Steps

1

Install BAML VSCode/Cursor Extension

Install the extension from: https://marketplace.visualstudio.com/items?itemName=boundary.baml-extensionFeatures include:
  • Syntax highlighting
  • Testing playground
  • Prompt previews
2

Install BAML CLI and Initialize Project

Install the BAML CLI tool globally and create starter BAML code:
cargo install baml-cli && baml-cli init
This will:
  1. Install the BAML CLI tool globally
  2. Create starter BAML code in a baml_src directory
  3. Set up the basic project structure
3

Install BAML Rust Runtime

Add the BAML runtime library to your project:
cargo add baml
4

Generate the baml_client Module

One of the files in your baml_src directory will have a generator block. Run this command to auto-generate the baml_client module with Rust code:
baml-cli generate
Any types defined in .baml files will be converted into Rust structs and enums.
If you have the VSCode extension installed, it will automatically run baml-cli generate when you save a BAML file.
5

Use BAML Functions in Rust

Import and use your generated BAML client:
main.rs
use myproject::baml_client::sync_client::B;
use myproject::baml_client::types::*;

fn main() {
    let raw_resume = "..."; // Your resume text

    // BAML's internal parser guarantees ExtractResume
    // to always return a Resume type or an error
    let resume = B.ExtractResume.call(raw_resume).unwrap();

    println!("Extracted resume: {:?}", resume);
}

fn example_stream(raw_resume: &str) -> Result<Resume, baml_client::Error> {
    let mut stream = B.ExtractResume.stream(raw_resume)?;

    for partial in stream.partials() {
        let partial = partial?;
        println!("Partial: {:?}", partial); // Partial Resume type
    }

    let final_result = stream.get_final_response()?;
    Ok(final_result) // Complete Resume type
}

Build Integration

You can modify your build process to always call baml-cli generate before building:
Makefile
.PHONY: generate build

generate:
	baml-cli generate

build: generate
	cargo build

test: generate
	cargo test

Working with Cargo

BAML integrates seamlessly with Cargo. Ensure your Cargo.toml includes the BAML dependency:
Cargo.toml
[package]
name = "myproject"
version = "0.1.0"
edition = "2024"

[dependencies]
baml = "0.203.1"
The generated baml_client module lives inside your crate:
// Choose one:
use myproject::baml_client::sync_client::B;   // Sync API
// or:
// use myproject::baml_client::async_client::B;   // Async API
use myproject::baml_client::types::*;          // Generated types

Error Handling

All BAML Rust function calls return Result<T, baml_client::Error>:
use myproject::baml_client::sync_client::B;

fn main() {
    let raw_resume = "...";
    match B.ExtractResume.call(raw_resume) {
        Ok(resume) => println!("Extracted: {:?}", resume),
        Err(e) => eprintln!("Error: {}", e),
    }
}

Cancellation and Timeouts

Rust uses CancellationToken for cancellation and timeouts:
use baml::CancellationToken;
use std::time::Duration;

// Set timeouts
let resume = "...";

let token = CancellationToken::new_with_timeout(Duration::from_secs(30));
let result = B.ExtractResume
    .with_cancellation_token(Some(token))
    .call(resume);

// Manual cancellation
let token = CancellationToken::new();
let token_clone = token.clone();

std::thread::spawn(move || {
    std::thread::sleep(Duration::from_secs(5));
    token_clone.cancel(); // Cancel after 5 seconds
});

let result = B.ExtractResume
    .with_cancellation_token(Some(token))
    .call(resume);

Build docs developers (and LLMs) love