client Option
info
Added in version 0.216.0
The client option provides a simple way to override which LLM client a function uses at runtime. It's a shorthand for creating a ClientRegistry and calling set_primary().
Basic Usage
- Python
- TypeScript
- Go
from baml_client import b
# Use shorthand provider/model syntax
result = await b.ExtractResume("...", baml_options={"client": "openai/gpt-4o-mini"})
# Use OpenRouter for open-source models
result = await b.ExtractResume("...", baml_options={"client": "openrouter/meta-llama/llama-3.1-70b-instruct"})
# Reference a client defined in .baml files
result = await b.ExtractResume("...", baml_options={"client": "MyCustomClient"})
# Use with_options to set for multiple calls
my_b = b.with_options(client="openai/gpt-4o-mini")
result1 = await my_b.ExtractResume("...")
result2 = await my_b.ExtractInvoice("...")
import { b } from "baml_client"
// Use shorthand provider/model syntax
const result = await b.ExtractResume("...", { client: "openai/gpt-4o-mini" })
// Use OpenRouter for open-source models
const result2 = await b.ExtractResume("...", { client: "openrouter/meta-llama/llama-3.1-70b-instruct" })
// Reference a client defined in .baml files
const result3 = await b.ExtractResume("...", { client: "MyCustomClient" })
// Use withOptions to set for multiple calls
const myB = b.withOptions({ client: "openai/gpt-4o-mini" })
const res1 = await myB.ExtractResume("...")
const res2 = await myB.ExtractInvoice("...")
import b "example.com/myproject/baml_client"
ctx := context.Background()
// Use shorthand provider/model syntax
result, err := b.ExtractResume(ctx, "...", b.WithClient("openai/gpt-4o-mini"))
// Use OpenRouter for open-source models
result, err = b.ExtractResume(ctx, "...", b.WithClient("openrouter/meta-llama/llama-3.1-70b-instruct"))
// Reference a client defined in .baml files
result, err = b.ExtractResume(ctx, "...", b.WithClient("MyCustomClient"))
When to Use
Use the client option when you want to:
- Quickly switch between different LLM providers or models
- A/B test different models
- Use different clients for different environments (dev vs prod)
- Override the default client defined in your
.bamlfiles
Precedence
If you provide both client and client_registry, the client option takes precedence:
- Python
- TypeScript
from baml_py import ClientRegistry
cr = ClientRegistry()
cr.set_primary("ModelA")
# "ModelB" will be used, not "ModelA"
result = await b.ExtractResume("...", baml_options={
"client": "ModelB",
"client_registry": cr
})
import { ClientRegistry } from "@boundaryml/baml"
const cr = new ClientRegistry()
cr.setPrimary("ModelA")
// "ModelB" will be used, not "ModelA"
const result = await b.ExtractResume("...", {
client: "ModelB",
clientRegistry: cr
})
Valid Client Names
The client value can be:
-
Shorthand provider/model syntax - Use
provider/modelformat:"openai/gpt-4o-mini"- OpenAI models"anthropic/claude-sonnet-4-20250514"- Anthropic models"openrouter/meta-llama/llama-3.1-70b-instruct"- OpenRouter models"google-ai/gemini-2.0-flash"- Google AI models
-
Client defined in
.bamlfiles - Reference by name (e.g.,"MyCustomClient")
If the client name is not found, you'll get a runtime error.
Comparison with ClientRegistry
| Feature | client option | ClientRegistry |
|---|---|---|
| Set primary client | Yes | Yes (set_primary) |
| Add custom clients | No | Yes (add_llm_client) |
| Override client options | No | Yes |
| Simplicity | Simple string | More complex |
Use client for simple overrides. Use ClientRegistry for adding new clients or customizing client options at runtime.
See Also
- with_options - Set default options for a client
- Client Configuration - Configure clients in BAML