Crate baml_sys

Crate baml_sys 

Source
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:

  1. Explicit path set via set_library_path()
  2. BAML_LIBRARY_PATH environment variable
  3. User cache directory (~/.cache/baml/libs/{VERSION}/)
  4. Auto-download from GitHub releases (if download feature enabled)
  5. 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 library
  • BAML_CACHE_DIR: Override cache directory location
  • BAML_LIBRARY_DISABLE_DOWNLOAD: Set to “true” to disable auto-download

Structs§

Buffer
Buffer returned from object operations.
Symbols
Loaded symbols from the dynamic library.

Enums§

BamlSysError
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§

CallbackFn
Callback function type for results.
OnTickCallbackFn
Callback function type for streaming ticks.
Result
Result type for baml-sys operations.