Rust
To set up BAML with Rust do the following:
Install BAML VSCode/Cursor Extension
https://marketplace.visualstudio.com/items?itemName=boundary.baml-extension
- syntax highlighting
- testing playground
- prompt previews
Install BAML CLI and Initialize Project
This command will:
- Install the BAML CLI tool globally
- Create starter BAML code in a
baml_srcdirectory - Set up the basic project structure
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.
You can modify your build process to always call baml-cli generate before building.
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.
Working with Cargo
BAML integrates seamlessly with Cargo. Make sure your Cargo.toml includes the BAML dependency:
The generated baml_client module lives inside your crate, so you can import it as:
Error Handling
All BAML Rust function calls return Result<T, baml_client::Error>, allowing idiomatic error handling:
Cancellation and Timeouts
Rust uses CancellationToken for cancellation and timeouts instead of a context parameter:
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.