OpenInference BAML Instrumentation
Python auto-instrumentation library for BAML (baml-py).
The traces emitted by this instrumentation follow the OpenInference semantic conventions and are fully OpenTelemetry compatible. They can be sent to any OpenTelemetry collector, such as Arize Phoenix.
Installation
pip install openinference-instrumentation-baml
Quickstart
In this example we will instrument a BAML application and observe traces via Arize Phoenix.
Install packages.
pip install openinference-instrumentation-baml "baml-py>=0.200" arize-phoenix-otel
Assuming you have a BAML project with a generated Python client (e.g. my_app.baml_client), instrument it as follows:
from phoenix.otel import register
tracer_provider = register(
batch=True,
auto_instrument=True, # automatically discovers openinference-instrumentation-baml
)
# That's it! All BAML function calls will now emit traces.
Or, if you prefer manual setup:
from openinference.instrumentation.baml import BamlInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
endpoint = "http://127.0.0.1:6006/v1/traces"
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
BamlInstrumentor().instrument(
tracer_provider=tracer_provider,
baml_client_module="my_app.baml_client",
)
Now run your application and observe the traces in Phoenix.
python your_file.py
How It Works
BAML generates a DoNotUseDirectlyCallManager class that all LLM function calls pass through. This instrumentor patches its call_function_async and call_function_sync methods to:
- Inject a per-call
Collectorto capture theFunctionLog - Extract trace data (model name, input/output messages, token counts, timing)
- Emit an OpenTelemetry span with OpenInference semantic conventions
Auto-Discovery
When using auto_instrument=True via phoenix.otel.register(), the instrumentor automatically scans loaded modules for a BAML generated client. This works as long as the baml_client module has been imported before register() is called.
If auto-discovery fails, pass the module explicitly:
BamlInstrumentor().instrument(
tracer_provider=tracer_provider,
baml_client_module="my_app.baml_client",
)
Captured Attributes
The following OpenInference attributes are populated on each span:
| Attribute | Source |
|---|---|
openinference.span.kind |
"LLM" |
llm.system |
"baml" |
llm.provider |
BAML client provider (e.g. "openai-generic") |
llm.model_name |
Model name from the HTTP request body |
llm.input_messages.* |
Parsed from the LLM request messages |
llm.output_messages.* |
Parsed from the LLM response choices |
llm.token_count.prompt |
Input token count |
llm.token_count.completion |
Output token count |
llm.token_count.total |
Sum of prompt + completion tokens |
llm.invocation_parameters |
Request parameters (temperature, max_tokens, etc.) |
input.value |
BAML function arguments (JSON) |
output.value |
LLM response content |
Limitations
Streaming calls are not instrumented. Only call_function_async and call_function_sync (non-streaming) are patched. For streaming calls (create_async_stream / create_sync_stream), the stream is consumed asynchronously by user code, so the instrumentor cannot reliably determine when the stream completes to capture the full response. Streaming support may be added in a future release.
Provider-specific attribute parsing. Input/output messages, model name, and invocation parameters are parsed from the raw HTTP request/response bodies, which vary by provider. The following providers are supported:
| Provider | llm.input_messages |
llm.output_messages |
llm.model_name |
llm.invocation_parameters |
Cache tokens |
|---|---|---|---|---|---|
openai, openai-generic, openrouter, ollama |
✓ | ✓ | ✓ | ✓ | cache_read (via BAML) |
anthropic |
✓ | ✓ | ✓ | ✓ | cache_read + cache_write |
For unsupported providers, a one-time warning is logged and these attributes are skipped. Token counts (prompt, completion, total, cache_read) are always extracted from BAML's provider-agnostic Usage object regardless of provider.
Disclaimer
This is not an official OpenInference library. It is a community-maintained extension and is provided as-is without warranty. The author is not responsible for any issues arising from its use.
The OpenInference project does not currently accept large-scale contributions, so this instrumentor is maintained separately. Contributions and feedback from the community are welcome. If the OpenInference team decides to build official BAML instrumentation in the future, users are encouraged to migrate to the official version.
More Info
Metadata
Release files for openinference-instrumentation-baml 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| openinference_instrumentation_baml-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for openinference_instrumentation_baml-0.2.0-py3-none-any.whl |
Release files / openinference_instrumentation_baml-0.2.0-py3-none-any.whl
| Download URL | openinference_instrumentation_baml-0.2.0-py3-none-any.whl |
|---|---|
| Size | 9.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ed7731ee35ae7ef03821bf4cf6f6920de55d730b1e15b3598aa45b385c53fa30
|
|
BLAKE2b-256 checksum How to use checksums |
d09d681fa9ed244a601f56bcc213c00cf6d61ba33d7eefaf3b62a32c919b5e43
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.13 {"installer":{"name":"uv","version":"0.11.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for openinference-instrumentation-baml 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.