Expand description
BAML FFI bindings with runtime dynamic library loading.
This crate provides low-level FFI bindings to the BAML runtime library.
The library is loaded dynamically at runtime using libloading.
§Library Resolution
The library is searched in the following order:
- Explicit path set via
set_library_path() BAML_LIBRARY_PATHenvironment variable- User cache directory (
~/.cache/baml/libs/{VERSION}/) - Auto-download from GitHub releases (if
downloadfeature enabled) - System default paths (
/usr/local/lib/, etc.)
§Usage
§Runtime Loading (Default)
ⓘ
use baml_sys::{get_symbols, version};
// Library is loaded on first access
let v = version()?;
println!("BAML version: {v}");§Build-time Hook
In your build.rs:
ⓘ
fn main() {
// Ensure library is available before build completes
let lib_path = baml_sys::ensure_library()
.expect("Failed to find/download BAML library");
println!("cargo:rerun-if-changed={}", lib_path.display());
}§Environment Variables
BAML_LIBRARY_PATH: Explicit path to the libraryBAML_CACHE_DIR: Override cache directory locationBAML_LIBRARY_DISABLE_DOWNLOAD: Set to “true” to disable auto-download
Structs§
Enums§
- Baml
SysError - Errors that can occur during library loading.
Constants§
- ENV_
CACHE_ DIR - Environment variable for cache directory override.
- ENV_
DISABLE_ DOWNLOAD - Environment variable to disable automatic download.
- ENV_
LIBRARY_ PATH - Environment variable for explicit library path.
- VERSION
- Package version from Cargo.toml (workspace).
Functions§
- call_
function_ ⚠from_ c - Call a BAML function.
- call_
function_ ⚠parse_ from_ c - Call a BAML function for parsing.
- call_
function_ ⚠stream_ from_ c - Call a BAML function with streaming.
- call_
object_ ⚠constructor - Call an object constructor.
- call_
object_ ⚠method - Call an object method.
- cancel_
function_ ⚠call - Cancel a function call.
- create_
baml_ ⚠runtime - Create a new BAML runtime.
- destroy_
baml_ ⚠runtime - Destroy a BAML runtime.
- ensure_
library - Ensure the library is available (for use in build.rs).
- free_
buffer ⚠ - Free a buffer returned from object operations.
- get_
symbols - Get the loaded symbols, initializing if necessary.
- invoke_
runtime_ ⚠cli - Invoke the runtime CLI.
- library_
path - Get the path to the loaded library.
- register_
callbacks ⚠ - Register callbacks for FFI operations.
- set_
library_ path - Set an explicit library path before initialization.
- version
- Get the BAML library version.
Type Aliases§
- Callback
Fn - Callback function type for results.
- OnTick
Callback Fn - Callback function type for streaming ticks.
- Result
- Result type for baml-sys operations.