title |
---|
with_options |
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.
logger = b.create_logger() client_registry = ClientRegistry() client_registry.set_primary("openai/gpt-4o-mini")
my_b = b.with_options(internal_logger=logger, client_registry=client_registry)
result = my_b.ExtractResume("...")
other_logger = b.create_logger() result2 = my_b.ExtractResume("...", baml_options={"logger": other_logger})
</Tab>
<Tab title="TypeScript">
```typescript
import { b } from "baml_client"
import { ClientRegistry } from "baml_client/client_registry"
// Set up default options for this client
const logger = b.createLogger()
const clientRegistry = new ClientRegistry()
clientRegistry.setPrimary("openai/gpt-4o-mini")
// Create client with default options
const myB = b.withOptions({ logger, clientRegistry })
// Uses the default options
const result = await myB.ExtractResume("...")
// Override options for a specific call
const otherLogger = b.createLogger()
const result2 = await myB.ExtractResume("...", { logger: otherLogger })
logger = Baml.Client.create_internal_logger client_registry = Baml::ClientRegistry.new client_registry.set_primary("openai/gpt-4o-mini")
my_b = Baml.Client.with_options(internal_logger: logger, client_registry: client_registry)
result = my_b.ExtractResume("...")
other_logger = Baml.Client.create_internal_logger result2 = my_b.ExtractResume("...", baml_options: { logger: other_logger })
</Tab>
</Tabs>
## 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.
<Tabs>
<Tab title="Python">
```python
from baml_client import b
from baml_py import ClientRegistry
def run():
# Configure options
logger = b.create_internal_logger()
client_registry = ClientRegistry()
client_registry.set_primary("openai/gpt-4o-mini")
# Create configured client
my_b = b.with_options(internal_logger=logger, client_registry=client_registry)
# All calls will use the configured options
res = my_b.ExtractResume("...")
invoice = my_b.ExtractInvoice("...")
# Access configuration
print(my_b.client_registry)
# Will print 2 logs from the logger
print(my_b.internal_logger.logs)
const logger = b.createLogger() const clientRegistry = new ClientRegistry() clientRegistry.setPrimary("openai/gpt-4o-mini")
const myB = b.withOptions({ logger, clientRegistry })
// All calls will use the configured options const res = await myB.ExtractResume("...") const invoice = await myB.ExtractInvoice("...")
// Access configuration console.log(myB.clientRegistry) console.log(myB.logger.last?.usage)
</Tab>
<Tab title="Ruby">
```ruby
require 'baml_client'
logger = Baml.Client.create_internal_logger
client_registry = Baml::ClientRegistry.new
client_registry.set_primary("openai/gpt-4o-mini")
my_b = Baml.Client.with_options(internal_logger: logger, client_registry: client_registry)
# All calls will use the configured options
res = my_b.ExtractResume("...")
invoice = my_b.ExtractInvoice("...")
# Access configuration
print(my_b.client_registry)
print(my_b.internal_logger.last.usage)
When running functions in parallel, with_options
helps maintain consistent configuration across all calls. This works seamlessly with the Logger
and id
functionality.
async def run(): my_b = b.with_options(client_registry=client_registry)
# Run multiple functions in parallel
res, invoice = await asyncio.gather(
my_b.ExtractResume("..."),
my_b.ExtractInvoice("...")
)
# Access results and logs
print(res)
print(invoice)
print(my_b.internal_logger.id(res.id).usage)
print(my_b.internal_logger.id(invoice.id).usage)
</Tab>
<Tab title="TypeScript">
```typescript
const myB = b.withOptions({ logger, clientRegistry })
// Run multiple functions in parallel
const [
{data: res, id: resumeId},
{data: invoice, id: invoiceId}
] = await Promise.all([
myB.raw.ExtractResume("..."),
myB.raw.ExtractInvoice("...")
])
// Access results and logs
console.log(res)
console.log(invoice)
console.log(myB.logger.id(resumeId)?.usage)
console.log(myB.logger.id(invoiceId)?.usage)
Async do my_b = Baml.Client.with_options(client_registry: client_registry)
# Run multiple functions in parallel
res, invoice = await [
my_b.ExtractResume("..."),
my_b.ExtractInvoice("...")
]
# Access results and logs
print(res)
print(invoice)
print(my_b.internal_logger.id(res.id).usage)
print(my_b.internal_logger.id(invoice.id).usage)
end
</Tab>
</Tabs>
### Streaming Mode
`with_options` can be used with streaming functions while maintaining all configured options.
<Tabs>
<Tab title="Python">
```python
from baml_client.async_client import b
async def run():
my_b = b.with_options(client_registry=client_registry)
stream = my_b.stream.ExtractResume("...")
async for chunk in stream:
print(chunk)
result = await stream.get_final_result()
print(my_b.internal_logger.id(stream.id).usage)
const stream = myB.stream.ExtractResume("...") for await (const chunk of stream) { console.log(chunk) }
const result = await stream.getFinalResult() console.log(myB.logger.id(stream.id)?.usage)
</Tab>
<Tab title="Ruby">
```ruby
require 'baml_client'
my_b = Baml.Client.with_options(client_registry: client_registry)
stream = my_b.stream.ExtractResume("...")
stream.each do |chunk|
print(chunk)
end
result = stream.get_final_result
print(my_b.internal_logger.id(stream.id).usage)
Parameter | Type | Description |
---|---|---|
logger / internal_logger |
Logger |
Logger instance for tracking function calls and usage |
client_registry |
ClientRegistry |
Registry for managing LLM clients and their configurations |
type_builder |
TypeBuilder |
Custom type builder for function inputs and outputs |
- Logger - Track function calls and usage metrics
- Function IDs - Track specific function calls
- TypeBuilder - Build custom types for your functions
- Client Registry - Manage LLM clients and their configurations
- Use
with_options
when you need consistent configuration across multiple function calls - Configure logging and client registry at the start of your application
- Create separate configured clients for different parts of your application that need different settings
- Use the configured client's properties to access logs and metrics