Rust

Set up BAML with Rust

To set up BAML with Rust do the following:

1

Install BAML VSCode/Cursor Extension

https://marketplace.visualstudio.com/items?itemName=boundary.baml-extension

  • syntax highlighting
  • testing playground
  • prompt previews
2

Install BAML CLI and Initialize Project

rust
$cargo install baml-cli && baml-cli init

This command 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

After initializing your project, add the BAML runtime library:

rust
$cargo add baml
4

Generate the baml_client Rust module from .baml files

One of the files in your baml_src directory will have a generator block. This tells BAML how to generate the baml_client module, which will have auto-generated Rust code to call your BAML functions.

Any types defined in .baml files will be converted into Rust structs and enums in the baml_client module.

$baml-cli generate

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

Makefile
1.PHONY: generate build
2
3generate:
4 baml-cli generate
5
6build: generate
7 cargo build
8
9test: generate
10 cargo test

See What is baml_client to learn more about how this works.

If you set up the VSCode extension, it will automatically run baml-cli generate on saving a BAML file.

5

Use a BAML function in Rust!

If baml_client doesn’t exist, make sure to run the previous step!
main.rs
1use myproject::baml_client::sync_client::B;
2use myproject::baml_client::types::*;
3
4fn main() {
5 let raw_resume = "..."; // Your resume text
6
7 // BAML's internal parser guarantees ExtractResume
8 // to always return a Resume type or an error
9 let resume = B.ExtractResume.call(raw_resume).unwrap();
10
11 println!("Extracted resume: {:?}", resume);
12}
13
14fn example_stream(raw_resume: &str) -> Result<Resume, baml_client::Error> {
15 let mut stream = B.ExtractResume.stream(raw_resume)?;
16
17 for partial in stream.partials() {
18 let partial = partial?;
19 println!("Partial: {:?}", partial); // This will be a partial Resume type
20 }
21
22 let final_result = stream.get_final_response()?;
23 Ok(final_result) // This will be a complete Resume type
24}

Working with Cargo

BAML integrates seamlessly with Cargo. Make sure your Cargo.toml includes the BAML dependency:

Cargo.toml
1[package]
2name = "myproject"
3version = "0.1.0"
4edition = "2024"
5
6[dependencies]
7baml = "0.203.1"

The generated baml_client module lives inside your crate, so you can import it as:

1// Choose one:
2use myproject::baml_client::sync_client::B; // Sync API
3// or:
4// use myproject::baml_client::async_client::B; // Async API
5use myproject::baml_client::types::*; // Generated types

Error Handling

All BAML Rust function calls return Result<T, baml_client::Error>, allowing idiomatic error handling:

1use myproject::baml_client::sync_client::B;
2
3fn main() {
4 let raw_resume = "..."; // Your resume text
5 match B.ExtractResume.call(raw_resume) {
6 Ok(resume) => println!("Extracted: {:?}", resume),
7 Err(e) => eprintln!("Error: {}", e),
8 }
9}

Cancellation and Timeouts

Rust uses CancellationToken for cancellation and timeouts instead of a context parameter:

1use baml::CancellationToken;
2use std::time::Duration;
3
4// Set timeouts
5let resume = "..."; // Your resume text
6
7let token = CancellationToken::new_with_timeout(Duration::from_secs(30));
8let result = B.ExtractResume
9 .with_cancellation_token(Some(token))
10 .call(resume);
11
12// Manual cancellation
13let token = CancellationToken::new();
14let token_clone = token.clone();
15
16std::thread::spawn(move || {
17 std::thread::sleep(Duration::from_secs(5));
18 token_clone.cancel(); // Cancel the request after 5 seconds
19});
20
21let result = B.ExtractResume
22 .with_cancellation_token(Some(token))
23 .call(resume);

You’re all set! Continue on to the Deployment Guides for your language to learn how to deploy your BAML code or check out the Interactive Examples to see more examples.