Skip to main content
BAML functions accept runtime options to control their behavior. Use options to override clients, add logging, customize environment variables, and more.

BamlCallOptions

All BAML functions accept options as their final parameter:
from baml_client import b

result = await b.ExtractResume(
    resume_text,
    baml_options={
        "client": "openai/gpt-4o-mini",
        "tags": {"user_id": "123"},
        "env": {"OPENAI_API_KEY": "sk-..."},
    }
)
Type Definition:
class BamlCallOptions(TypedDict, total=False):
    tb: TypeBuilder
    client_registry: ClientRegistry
    client: str
    collector: Collector | list[Collector]
    env: dict[str, str | None]
    tags: dict[str, str]
    on_tick: Callable[[str, FunctionLog | None], None]

Option Reference

client

Type: string Override which LLM client to use for this call. Supports:
  • Shorthand syntax: "provider/model" (e.g., "openai/gpt-4o", "anthropic/claude-sonnet-4-20250514")
  • Named client: Reference a client defined in your .baml files
# Use shorthand syntax
result = await b.ExtractResume(
    resume_text,
    baml_options={"client": "openai/gpt-4o-mini"}
)

# Use named client from .baml files
result = await b.ExtractResume(
    resume_text,
    baml_options={"client": "MyCustomClient"}
)
See the client option documentation above for more details.

client_registry

Type: ClientRegistry Provide a custom client registry for advanced client configuration. Allows you to:
  • Add new LLM clients at runtime
  • Override client settings
  • Set a primary client
from baml_py import ClientRegistry

cr = ClientRegistry()
cr.add_llm_client(
    "MyDynamicClient",
    provider="openai",
    options={
        "model": "gpt-4o",
        "api_key": "sk-...",
    }
)
cr.set_primary("MyDynamicClient")

result = await b.ExtractResume(
    resume_text,
    baml_options={"client_registry": cr}
)
Note: If both client and client_registry are provided, client takes precedence.

collector

Type: Collector | Collector[] Attach collectors to track and analyze function execution. Collectors capture:
  • Function inputs and outputs
  • Prompts and LLM responses
  • Token usage
  • Latency metrics
from baml_py import Collector

collector = Collector("my-collector")

result = await b.ExtractResume(
    resume_text,
    baml_options={"collector": collector}
)

# Access collected data
print(collector.last.usage.input_tokens)
print(collector.last.usage.output_tokens)

env

Type: Record<string, string | undefined> Override environment variables for this specific call. Useful for:
  • Using different API keys
  • Testing with different configurations
  • Multi-tenant applications
result = await b.ExtractResume(
    resume_text,
    baml_options={
        "env": {
            "OPENAI_API_KEY": "sk-tenant-123",
            "OPENAI_BASE_URL": "https://custom.openai.com",
        }
    }
)

tags

Type: Record<string, string> Add metadata tags to function calls for filtering and analysis in observability tools.
result = await b.ExtractResume(
    resume_text,
    baml_options={
        "tags": {
            "user_id": "user-123",
            "environment": "production",
            "feature": "resume-parser",
        }
    }
)

on_tick

Type: (reason: string, log: FunctionLog | null) => void Receive real-time callbacks during function execution. Useful for:
  • Monitoring progress
  • Debugging
  • Accessing streaming metadata
from baml_py import FunctionLog

def on_tick(reason: str, log: FunctionLog | None):
    if log:
        print(f"Tick: {reason}, Calls: {len(log.calls)}")

result = await b.ExtractResume(
    resume_text,
    baml_options={"on_tick": on_tick}
)
See the OnTick documentation above for more details.

signal (TypeScript only)

Type: AbortSignal Cancel function execution using an abort controller.
const controller = new AbortController()

// Start the call
const promise = b.ExtractResume(resumeText, {
    signal: controller.signal
})

// Cancel after 5 seconds
setTimeout(() => controller.abort(), 5000)

try {
    const result = await promise
} catch (error) {
    if (error instanceof BamlAbortError) {
        console.log('Call was aborted')
    }
}
See the Abort Signal documentation above for more details.

tb (TypeBuilder)

Type: TypeBuilder Dynamically modify types at runtime. Advanced feature for dynamic schema generation.
from baml_client.type_builder import TypeBuilder

tb = TypeBuilder()
tb.add_class("CustomResume").add_field("name", "string")

result = await b.ExtractResume(
    resume_text,
    baml_options={"tb": tb}
)

Using with_options / withOptions

Set default options for multiple function calls:
from baml_client import b

# Create a client with default options
my_b = b.with_options(
    client="openai/gpt-4o-mini",
    tags={"environment": "production"},
)

# All calls use these defaults
result1 = await my_b.ExtractResume(resume_text)
result2 = await my_b.ExtractInvoice(invoice_text)

# Override per-call
result3 = await my_b.ExtractResume(
    resume_text,
    baml_options={"client": "openai/gpt-4o"}
)

Default Values

OptionDefault
clientClient specified in BAML function
client_registryNone / null
collectorNone / null
envProcess environment variables
tags{} (empty)
on_tickNone / null
signalundefined
tbNone / null

Build docs developers (and LLMs) love