Skip to main content

with_options

info

Added in version 0.79.0

Creates a new client with default configuration options for logging, client registry, and type builders. These options are automatically applied to all function calls made through this client, but can be overridden on a per-call basis.

Syntax​

from baml_client import b
from baml_py import ClientRegistry, Collector

my_b = b.with_options(
client="openai/gpt-4o",
collector=collector,
client_registry=client_registry,
env={"BAML_LOG": "DEBUG"}
)

Parameters​

ParameterTypeDescription
clientstringClient name to use for all calls (shorthand for client_registry.set_primary())
collectorCollectorCollector instance for tracking function calls and usage metrics
client_registryClientRegistryRegistry for managing LLM clients and their configurations
type_builderTypeBuilderCustom type builder for function inputs and outputs
envDict/ObjectEnvironment variables to set for the client
note

All options can be overridden on a per-call basis using the baml_options parameter.

Examples​

Basic Configuration​

from baml_client import b
from baml_py import ClientRegistry, Collector

collector = Collector(name="my-collector")
client_registry = ClientRegistry()
client_registry.set_primary("openai/gpt-4o")

my_b = b.with_options(collector=collector, client_registry=client_registry)

# All calls use the configured options
res = my_b.ExtractResume("...")
invoice = my_b.ExtractInvoice("...")

# Access logs from the collector
print(collector.logs)
print(collector.last.usage)

Per-call Tags​

Add tags to a specific BAML function call for correlation, A/B testing, or user tracking:

from baml_client import b
from baml_py import Collector

collector = Collector(name="tags-collector")
res = b.TestFunction(
"hello",
baml_options={
"collector": collector,
"tags": {"call_id": "first", "version": "v1"},
},
)

print(collector.last.tags)

Parallel Execution​

from baml_client.async_client import b
from baml_py import Collector
import asyncio

async def run():
collector = Collector(name="my-collector")
my_b = b.with_options(collector=collector)

# Run multiple functions in parallel
res, invoice = await asyncio.gather(
my_b.ExtractResume("..."),
my_b.ExtractInvoice("...")
)

# Iterate logs to see usage for each call
for log in collector.logs:
print(log.usage)

Streaming Mode​

from baml_client.async_client import b
from baml_py import Collector

async def run():
collector = Collector(name="my-collector")
my_b = b.with_options(collector=collector)

stream = my_b.stream.ExtractResume("...")
async for chunk in stream:
print(chunk)

result = await stream.get_final_result()
print(collector.last.usage)

See Also​