BAML supports getting structured output from all major LLM providers and OpenAI-API compatible open-source models. This guide shows you how to switch between different models and providers.
BAML can help you get structured output from any Open-Source model, with better performance than other techniques, even when it’s not officially supported via a Tool-Use API or fine-tuned for it. Read more about Schema-Aligned Parsing.
Switch models at runtime using the Client Registry:
Python
TypeScript
from baml_client import bfrom baml_py import ClientRegistryimport osasync def run(): cr = ClientRegistry() # Add a new client at runtime cr.add_llm_client( name='GPT4', provider='openai', options={ "model": "gpt-4", "temperature": 0.7, "api_key": os.environ.get('OPENAI_API_KEY') } ) # Use the runtime client cr.set_primary('GPT4') result = await b.MakeHaiku("mountains", {"client_registry": cr})
import { b } from './baml_client'import { ClientRegistry } from '@boundaryml/baml'async function run() { const cr = new ClientRegistry() // Add a new client at runtime cr.addLlmClient('GPT4', 'openai', { model: "gpt-4", temperature: 0.7, api_key: process.env.OPENAI_API_KEY }) // Use the runtime client cr.setPrimary('GPT4') const result = await b.MakeHaiku("mountains", { clientRegistry: cr })}