setPrompt or addContext calls needed. Wrap a BAML method with wrapBAML / wrap_baml and Bitfab extracts everything automatically.
Canonical signatures: TypeScript wrapBAML · Python wrap_baml
Supported Languages
Quick Start
What Gets Captured
wrapBAML / wrap_baml creates a BAML Collector, runs the method through a tracked client, then extracts:
If
@boundaryml/baml (TypeScript) or baml-py (Python) is not installed, the BAML method is called directly without instrumentation.
Limitations
- Streaming functions are not auto-instrumented.
wrapBAML/wrap_bamlawaits a single result, so streaming calls (b.stream.ClassifyText, which return a stream rather than a final value) are not captured. Instrument the non-streaming form (b.ClassifyText), or capture the call manually withsetPrompt/addContextinside awithSpan/@spanroot after draining the stream. - Functions that make more than one LLM call capture only one of them. When a BAML function issues multiple HTTP calls (retry policies,
fallbackclients, round-robin clients), the captured prompt, model, and provider come from the selected call (or the first one if none is marked selected). Token usage is taken from the Collector’s aggregate, so on a fallback chain the recordedmodel/providermay not correspond to the call that produced the tokens.
TypeScript
Installation
Method Signature
method(Function, required) - The BAML method to wrap (e.g.,b.ClassifyText)bamlClient(unknown, optional) - The BAML client instance. Required if not passed in theBitfabconstructoroptions(WrapBAMLOptions, optional) - Configuration options
onCollector?: (collector: unknown) => void- Callback fired after each invocation with the BAML Collector instance
WrappedBamlFn - an async function with the same signature as the original, plus a .collector property.
Usage
Constructor-Based (Recommended)
Explicit Client
Accessing the BAML Collector
The wrapped function exposes a.collector property containing the BAML Collector from the most recent call:
.collector is null before the first call or if @boundaryml/baml is not installed.
onCollector Callback
For more control, pass an onCollector callback:
Error Handling
If@boundaryml/baml is not installed, wrapBAML falls back to calling the method directly on the BAML client without instrumentation. Metadata extraction errors are silently caught.
Python
Installation
Method Signature
method(Callable, required) - The BAML method to wrap (e.g.,b.ClassifyText)baml_client(Any, optional) - The BAML client instance. Required if not passed in theBitfabconstructoron_collector(Callable, optional, keyword-only): Callback fired after each invocation with the BAMLCollectorinstance
.collector attribute holding the most recent call’s Collector (None before the first call or if baml-py is not installed).
Usage
Constructor-Based (Recommended)
Explicit Client
Accessing the BAML Collector
The wrapper exposes a.collector attribute with the BAML Collector from the most recent call, and accepts an on_collector callback fired after each invocation:
Error Handling
Ifbaml-py is not installed, wrap_baml falls back to calling the method directly on the BAML client without instrumentation. Metadata extraction errors are silently caught.