with_options

Added in 0.79.0

The with_options function 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 when needed.

Quick Start

1from baml_client import b
2from baml_py import ClientRegistry, Collector
3
4# Set up default options for this client
5collector = Collector(name="my-collector")
6client_registry = ClientRegistry()
7client_registry.set_primary("openai/gpt-4o-mini")
8
9# Create client with default options
10my_b = b.with_options(collector=collector, client_registry=client_registry)
11
12# Uses the default options
13result = my_b.ExtractResume("...")
14
15# Override options for a specific call
16other_collector = Collector(name="other-collector")
17result2 = my_b.ExtractResume("...", baml_options={"collector": other_collector})

Common Use Cases

Basic Configuration

Use with_options to create a client with default settings that will be applied to all function calls made through this client. These defaults can be overridden when needed.

1from baml_client import b
2from baml_py import ClientRegistry, Collector
3
4def run():
5 # Configure options
6 collector = Collector(name="my-collector")
7 client_registry = ClientRegistry()
8 client_registry.set_primary("openai/gpt-4o-mini")
9
10 # Create configured client
11 my_b = b.with_options(collector=collector, client_registry=client_registry)
12
13 # All calls will use the configured options
14 res = my_b.ExtractResume("...")
15 invoice = my_b.ExtractInvoice("...")
16
17 # Access configuration
18 print(my_b.client_registry)
19 # Access logs from the collector
20 print(collector.logs)
21 print(collector.last)

Parallel Execution

When running functions in parallel, with_options helps maintain consistent configuration across all calls. This works seamlessly with the Collector functionality.

1from baml_client.async_client import b
2from baml_py import ClientRegistry, Collector
3import asyncio
4
5async def run():
6 collector = Collector(name="my-collector")
7 my_b = b.with_options(collector=collector, client_registry=client_registry)
8
9 # Run multiple functions in parallel
10 res, invoice = await asyncio.gather(
11 my_b.ExtractResume("..."),
12 my_b.ExtractInvoice("...")
13 )
14
15 # Access results and logs
16 print(res)
17 print(invoice)
18 print(collector.id(res.id).usage)
19 print(collector.id(invoice.id).usage)

Streaming Mode

with_options can be used with streaming functions while maintaining all configured options.

1from baml_client.async_client import b
2from baml_py import Collector
3
4async def run():
5 collector = Collector(name="my-collector")
6 my_b = b.with_options(collector=collector, client_registry=client_registry)
7
8 stream = my_b.stream.ExtractResume("...")
9 async for chunk in stream:
10 print(chunk)
11
12 result = await stream.get_final_result()
13 print(collector.id(stream.id).usage)

API Reference

with_options Parameters

These can always be overridden on a per-call basis with the baml_options parameter in any function call.

ParameterTypeDescription
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

Configured Client Properties

The configured client maintains the same interface as the base baml_client, so you can use all the same functions and methods.

The configured client maintains the same interface as the base client, so you can use all the same functions and methods.