Client Registry
If you need to modify the model / parameters for an LLM client at runtime, you can modify the ClientRegistry for any specified function.
Quick Override: If you just need to change which client a function uses, you can use the simpler client option instead of creating a full ClientRegistry:
Python
TypeScript
Ruby
Go
OpenAPI
The set_primary Method
The set_primary method can be called with either one of the clients added to
the ClientRegistry at runtime using add_llm_client or a client defined in
BAML files.
set_primary simply selects a client from all the available clients in the ClientRegistry,
but it does not mean there will be a “secondary” client used anywhere.
You can, however, define a fallback client and then use ClientRegistry to set
that client as the calling client for BAML functions. Here’s a simple example:
Then, in your code, you can use the ClientRegistry to set the GptOpusFallback at
runtime, which will try to use GPT 5 and if it fails Opus 4:
Python
TypeScript
Ruby
Go
Now the calling client will be GptOpusFallback, nothing else.
ClientRegistry Interface
Note: ClientRegistry is imported from baml_py in Python and @boundaryml/baml in TypeScript, not baml_client.
As we mature ClientRegistry, we will add a more type-safe and ergonomic interface directly in baml_client. See Github issue #766.
Methods use snake_case in Python and camelCase in TypeScript.
add_llm_client / addLlmClient
A function to add an LLM client to the registry.
The name of the client.
Using the exact same name as a client also defined in .baml files overwrites the existing client whenever the ClientRegistry is used.
This configures which provider to use. The provider is responsible for handling the actual API calls to the LLM service. The provider is a required field.
The configuration modifies the URL request BAML runtime makes.
A non-exhaustive list of providers you can use with openai-generic:
We also have some special providers that allow composing clients together:
These vary per provider. Please see provider specific documentation for more information. Generally they are pass through options to the POST request made to the LLM.
The name of a retry policy that is already defined in a .baml file. See Retry Policies.
set_primary / setPrimary
This sets the client for the function to use. (i.e. replaces the client property in a function)
The name “primary” does not imply that there will be a “secondary” or fallback
client used anywhere. It simply means that you choose one client from all the
available clients in your ClientRegistry.
See The set_primary Method section for more details.
The name of the client to use.
This can be a new client that was added with add_llm_client or an existing client that is already in a .baml file.