### Start Server with Custom Settings Source: https://docs.boundaryml.com/ref/baml-cli/serve Example of starting the BAML API server with a custom source directory and port. The `--from` option specifies the path to `baml_src`, and `--port` sets the listening port. ```bash baml-cli serve --from /path/to/my/baml_src --port 3000 --preview ``` -------------------------------- ### Install BAML CLI and Initialize Go Project Source: https://docs.boundaryml.com/guide/installation-language/go Installs the BAML CLI globally and creates a starter BAML project structure with initial code. This is the first step for any new BAML project. ```bash go install github.com/boundaryml/baml/baml-cli@latest && baml-cli init ``` -------------------------------- ### Install OpenAPI Dependencies on Linux (apt) Source: https://docs.boundaryml.com/guide/installation-language/rest-api-other-languages Install npm and the default JDK via apt. ```bash apt install npm default-jdk -y # 'npm' will install npx; 'default-jdk' will install java ``` -------------------------------- ### Install BAML CLI with deno Source: https://docs.boundaryml.com/guide/installation-language/typescript Use this command to install the BAML CLI package using deno. ```bash deno install npm:@boundaryml/baml ``` -------------------------------- ### Finish Reason Allow List Example Source: https://docs.boundaryml.com/ref/llm-client-providers/aws-bedrock Provides an example of how to configure `finish_reason_allow_list` to restrict allowed finish reasons. This example sets the list to only allow the 'stop' reason, treating others as failures. ```baml finish_reason_allow_list: ["stop"] ``` -------------------------------- ### Start BAML Language Server Source: https://docs.boundaryml.com/guide/installation-editors/others Starts the BAML LSP server on stdio for manual editor integration. ```bash baml-cli lsp ``` -------------------------------- ### Install BAML CLI with bun Source: https://docs.boundaryml.com/guide/installation-language/typescript Use this command to install the BAML CLI package using bun. ```bash bun add @boundaryml/baml ``` -------------------------------- ### Initialize BAML Project Source: https://docs.boundaryml.com/guide/installation-language/rust Install the BAML CLI globally and initialize the project structure. ```bash cargo install baml-cli && baml-cli init ``` -------------------------------- ### Agent Output Example Source: https://docs.boundaryml.com/examples/prompt-engineering/tools-function-calling Sample console output demonstrating successful tool invocation. ```text Agent started! Type 'exit' to quit. You: What's the weather in Seattle Agent (Weather): The weather in Seattle at 2023-10-02T12:00:00Z is sunny. You: What's 5+2 Agent (Calculator): The result is 7.0 ``` -------------------------------- ### Install BAML CLI with npm Source: https://docs.boundaryml.com/guide/installation-language/typescript Use this command to install the BAML CLI package using npm. ```bash npm install @boundaryml/baml ``` -------------------------------- ### Start BAML Development Server Source: https://docs.boundaryml.com/ref/baml-cli/dev Use this command to start a development server that watches BAML source files for changes and automatically reloads the BAML runtime. This is useful for rapid development and testing. ```bash baml-cli dev [OPTIONS] ``` -------------------------------- ### Install BAML CLI with yarn Source: https://docs.boundaryml.com/guide/installation-language/typescript Use this command to install the BAML CLI package using yarn. ```bash yarn add @boundaryml/baml ``` -------------------------------- ### Verify OpenAPI Installation on Windows Source: https://docs.boundaryml.com/guide/installation-language/rest-api-other-languages Check that npx and java are correctly installed and accessible in the path. ```powershell npx -version java -version ``` -------------------------------- ### Install BAML Dependencies Source: https://docs.boundaryml.com/guide/framework-integration/react-next-js/quick-start Install the BAML package and the Next.js plugin using your preferred package manager. ```bash npm install @boundaryml/baml @boundaryml/baml-nextjs-plugin ``` ```bash pnpm add @boundaryml/baml @boundaryml/baml-nextjs-plugin ``` ```bash yarn add @boundaryml/baml @boundaryml/baml-nextjs-plugin ``` ```bash bun add @boundaryml/baml @boundaryml/baml-nextjs-plugin ``` ```bash deno add @boundaryml/baml @boundaryml/baml-nextjs-plugin ``` -------------------------------- ### Install BAML Package Source: https://docs.boundaryml.com/guide/installation-language/python Install the baml-py package using your preferred package manager. ```bash pip install baml-py ``` ```bash poetry add baml-py ``` ```bash uv add baml-py ``` -------------------------------- ### Start BAML HTTP API Server Source: https://docs.boundaryml.com/ref/baml-cli/serve Use this command to start the BAML API server. It exposes your BAML functions via HTTP endpoints. Consider using the `dev` command for active development with hot-reloading. ```bash baml-cli serve [OPTIONS] ``` ```bash baml-cli dev [OPTIONS] ``` -------------------------------- ### Configure Web Search Tools Source: https://docs.boundaryml.com/ref/llm-client-providers/open-ai-responses-api Example of enabling web search tools within the client configuration. ```BAML client WebSearchClient { provider openai-responses options { model "gpt-4.1" tools [ { type "web_search_preview" } ] } } ``` -------------------------------- ### Install OpenAPI Dependencies on Linux (yum/dnf) Source: https://docs.boundaryml.com/guide/installation-language/rest-api-other-languages Install npm and Java using dnf or yum package managers. ```bash dnf install npm java-21-openjdk -y # dnf is the successor to yum ``` ```bash dnf install npm java-21-amazon-corretto -y # 'npm' will install npx # 'java-21-amazon-corretto' will install java ``` ```bash curl -sL https://rpm.nodesource.com/setup_16.x | bash - yum install nodejs -y # 'nodejs' will install npx amazon-linux-extras install java-openjdk11 -y # 'java-openjdk11' will install java ``` -------------------------------- ### Configure Client with Finish Reason Allow List Source: https://docs.boundaryml.com/ref/llm-client-providers/anthropic Example of configuring a client to only allow specific finish reasons. ```baml client MyClient { ``` -------------------------------- ### Install BAML CLI with pnpm Source: https://docs.boundaryml.com/guide/installation-language/typescript Use this command to install the BAML CLI package using pnpm. ```bash pnpm add @boundaryml/baml ``` -------------------------------- ### Implement Image Analysis Source: https://docs.boundaryml.com/ref/llm-client-providers/open-ai-responses-api Example of setting up a vision-capable client and a function to analyze image inputs. ```BAML client OpenAIResponsesVision { provider openai-responses options { model "gpt-4.1" } } function AnalyzeImage(image: image|string) -> string { client OpenAIResponsesVision prompt #" {{ _.role("user") }} What is in this image? {{ image }} "# } ``` -------------------------------- ### BAML Serve Command Usage Source: https://docs.boundaryml.com/ref/baml-cli/serve Information on how to use the `serve` and `dev` commands for starting the BAML API server. ```APIDOC ## BAML Serve Command The `serve` command starts a BAML-over-HTTP API server that exposes your BAML functions via HTTP endpoints. ### Usage ```bash baml-cli serve [OPTIONS] ``` For active development with hot-reloading, use the `dev` command: ```bash baml-cli dev [OPTIONS] ``` ### Options | Option | Description | Default | | -------------------- | ------------------------------------------------------------ | ------------ | | `--from ` | Path to the `baml_src` directory | `./baml_src` | | `--port ` | Port to expose BAML on | `2024` | | `--no-version-check` | Generate `baml_client` without checking for version mismatch | `false` | ### Examples 1. Start the server with default settings: ```bash baml-cli serve --preview ``` 2. Start the server with a custom source directory and port: ```bash baml-cli serve --from /path/to/my/baml_src --port 3000 --preview ``` ``` -------------------------------- ### Install Go Imports Tool Source: https://docs.boundaryml.com/guide/installation-language/go Installs the `goimports` tool, which is required by the BAML generator to format generated Go code. `gofmt` is included with Go by default. ```bash # gofmt comes with Go by default, but install goimports go install golang.org/x/tools/cmd/goimports@latest ``` -------------------------------- ### Start BAML Development Server Source: https://docs.boundaryml.com/guide/installation-language/rest-api-other-languages Starts the BAML development server, which serves BAML functions over REST, generates an OpenAPI schema, and creates an OpenAPI client. The server automatically reloads on file changes. ```bash npx @boundaryml/baml dev --preview ``` -------------------------------- ### Install BAML Runtime Source: https://docs.boundaryml.com/guide/installation-language/rust Add the BAML runtime library to your Rust project. ```bash cargo add baml ``` -------------------------------- ### Configure and Use BAML Client in PHP Source: https://docs.boundaryml.com/guide/installation-language/rest-api-other-languages Setup composer dependencies and initialize the API client for PHP projects. ```json "repositories": [ { "type": "path", "url": "baml_client" } ], "require": { "boundaryml/baml-client": "*@dev" } ``` ```php org.openapitools openapi-generator-maven-plugin 7.8.0 generate ${project.basedir}/baml_client/openapi.yaml baml ${project.build.directory}/generated-sources/openapi com.boundaryml.baml_client.api com.boundaryml.baml_client.model com.boundaryml.baml_client true ``` ```kotlin plugins { id("org.openapi.generator") version "7.8.0" } openApiGenerate { generatorName.set("java") // Change to 'kotlin', 'spring', etc. if needed inputSpec.set("${projectDir}/baml_client/openapi.yaml") outputDir.set("$buildDir/generated-sources/openapi") apiPackage.set("com.boundaryml.baml_client.api") modelPackage.set("com.boundaryml.baml_client.model") invokerPackage.set("com.boundaryml.baml_client") additionalProperties.set(mapOf("java8" to "true")) } sourceSets["main"].java { srcDir("$buildDir/generated-sources/openapi/src/main/java") } tasks.named("compileJava") { dependsOn("openApiGenerate") } ``` -------------------------------- ### Placeholder for other languages Source: https://docs.boundaryml.com/guide/introduction/what-is-baml A placeholder indicating that installation guides are available for other languages to set up BAML. ```text # read the installation guide for other languages! ``` -------------------------------- ### Initialize BAML project Source: https://docs.boundaryml.com/guide/installation-language/ruby Create the baml_src directory with starter configuration. ```bash bundle exec baml-cli init ``` -------------------------------- ### Integrating Multiple AI Providers with AI SDK Source: https://docs.boundaryml.com/guide/comparisons/baml-vs-ai-sdk Shows the setup required to use multiple AI providers (OpenAI, Anthropic, Google) with AI SDK. This involves installing provider-specific packages, importing from different modules, and implementing logic to select the appropriate model based on the provider. ```bash npm install @ai-sdk/openai @ai-sdk/anthropic @ai-sdk/google @ai-sdk/mistral ``` ```typescript import { openai } from '@ai-sdk/openai'; import { anthropic } from '@ai-sdk/anthropic'; import { google } from '@ai-sdk/google'; function getModel(provider: string) { switch(provider) { case 'openai': return openai('gpt-4o'); case 'anthropic': return anthropic('claude-3-opus-20240229'); case 'google': return google('gemini-pro'); // Don't forget to handle errors... } } const providers = { openai: process.env.OPENAI_API_KEY, anthropic: process.env.ANTHROPIC_API_KEY, google: process.env.GOOGLE_API_KEY, // More environment variables to manage... }; ``` -------------------------------- ### Initialize BAML Project Source: https://docs.boundaryml.com/guide/installation-language/python Create the baml_src directory and starter files in an existing project. ```bash baml-cli init ``` ```bash poetry run baml-cli init ``` ```bash uv run baml-cli init ``` -------------------------------- ### Dynamic Class Example Source: https://docs.boundaryml.com/ref/baml_client/type-builder Example of defining a dynamic class with properties. ```APIDOC ```baml class User { name string age int @@dynamic // Allow adding more properties } ``` ```python tb = TypeBuilder() tb.User.add_property("email", tb.string()) ``` ``` -------------------------------- ### Client Configuration with Finish Reason Lists Source: https://docs.boundaryml.com/ref/llm-client-providers/open-ai Demonstrates how to configure a BAML client with options for controlling finish reasons, using either an allow list or a deny list. ```APIDOC ## Client Configuration with Finish Reason Lists ### Description Configure a BAML client to manage the finish reasons returned by the language model. You can specify a list of allowed finish reasons or a list of denied finish reasons. Note that only one of these lists can be set at a time. ### Method N/A (Configuration Block) ### Endpoint N/A (Configuration Block) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```baml client MyClient { provider "openai" options { model "gpt-5-mini" api_key env.OPENAI_API_KEY // Finish reason allow list will only allow the stop finish reason finish_reason_allow_list ["stop"] } } ``` ### Request Example (Deny List) ```baml client MyClient { provider "openai" options { model "gpt-5-mini" api_key env.OPENAI_API_KEY // Finish reason deny list will allow all finish reasons except length finish_reason_deny_list ["length"] } } ``` ### Response N/A (Configuration Block) #### Success Response (200) N/A #### Response Example N/A ### Error Handling - `BamlClientFinishReasonError`: Raised if the finish reason is in the `finish_reason_deny_list`. ``` -------------------------------- ### Install OpenAPI Dependencies on macOS Source: https://docs.boundaryml.com/guide/installation-language/rest-api-other-languages Use Homebrew to install npm and the OpenAPI generator. ```bash brew install npm openapi-generator # 'npm' will install npx # 'openapi-generator' will install both Java and openapi-generator-cli ``` -------------------------------- ### Setup API Keys Source: https://docs.boundaryml.com/guide/framework-integration/react-next-js/quick-start Configure your API keys for BAML providers in your `.env.local` file. For BAML Observability, also include `BOUNDARY_API_KEY`. ```.env OPENAI_API_KEY=sk-... ``` ```.env.local BOUNDARY_API_KEY=your_api_key_here OPENAI_API_KEY=sk-... ``` -------------------------------- ### Initialize BAML Project for Native Go Client Source: https://docs.boundaryml.com/ref/baml-cli/init This command initializes a BAML project with a native Go client, which is recommended for Go projects. Use the `--client-type go` option. ```bash baml init --client-type go ``` -------------------------------- ### Configure Client with Role Metadata and Beta Header Source: https://docs.boundaryml.com/ref/llm-client-providers/anthropic Example of setting allowed role metadata and a specific beta header for prompt caching. ```baml client ClaudeWithCaching { provider anthropic options { model claude-3-5-haiku-20241022 api_key env.ANTHROPIC_API_KEY max_tokens 1000 allowed_role_metadata ["cache_control"] headers { "anthropic-beta" "prompt-caching-2024-07-31" } } } ``` -------------------------------- ### Error Detection Examples Source: https://docs.boundaryml.com/ref/baml_client/errors/baml-abort-error Provides code examples for detecting and handling BamlAbortError in different programming languages. ```APIDOC ## Error Detection ### TypeScript ```typescript import { BamlAbortError } from '@/baml_client' try { const result = await b.FunctionName(input, { abortController: controller }) } catch (error) { // Method 1: instanceof check (recommended) if (error instanceof BamlAbortError) { console.log('Operation was cancelled') } // Method 2: name check (works with minification) if (error.name === 'BamlAbortError') { console.log('Operation was cancelled') } } ``` ### Python ```python from baml_py import BamlAbortError try: result = await b.FunctionName( input, baml_options={"abort_controller": controller} ) except BamlAbortError as e: print(f"Operation was cancelled: {e}") if e.reason: print(f"Reason: {e.reason}") except Exception as e: # Handle other errors raise ``` ### Go ```go import ( "context" "errors" ) result, err := b.FunctionName(ctx, input) if err != nil { if errors.Is(err, context.Canceled) { // Direct cancellation fmt.Println("Operation was cancelled") } else if errors.Is(err, context.DeadlineExceeded) { // Timeout-based cancellation fmt.Println("Operation timed out") } else { // Other error return err } } ``` ``` -------------------------------- ### Initialize BAML project with deno Source: https://docs.boundaryml.com/guide/installation-language/typescript Run this command to initialize a new BAML project structure in your existing project using deno. ```bash deno run -A npm:@boundaryml/baml/baml-cli init ``` -------------------------------- ### Initialize BAML Project (Default) Source: https://docs.boundaryml.com/ref/baml-cli/init Use this command to initialize a BAML project in the current directory with default settings. No additional arguments are required. ```bash baml init ``` -------------------------------- ### Install Pinecone Package Source: https://docs.boundaryml.com/examples/prompt-engineering/retrieval-augmented-generation Install the Pinecone client library using pip. This is required to use Pinecone as your vector database for the RAG pipeline. ```bash pip install pinecone ``` -------------------------------- ### Incorrect GPT-4 JSON Output Example Source: https://docs.boundaryml.com/guide/comparisons/baml-vs-pydantic An example of potentially incorrect or overly verbose JSON output from GPT-4, which can lead to confusion and increased token usage. ```json { "name": { "title": "Name", "type": "string", "value": "John Doe" }, "skills": { "items": { "type": "string", "values": [ "Python", "JavaScript", "React" ] ... } } ``` -------------------------------- ### Makefile for BAML Project Build Source: https://docs.boundaryml.com/guide/installation-language/go Example Makefile demonstrating how to integrate `baml-cli generate` into your build process. Ensures the client package is generated before building or testing. ```makefile .PHONY: generate build generate: baml-cli generate build: generate go build ./... test: generate go test ./... ``` -------------------------------- ### Initialize BAML Project with OpenAPI Client Source: https://docs.boundaryml.com/guide/installation-language/rest-api-other-languages Use this command to initialize a BAML project and specify the OpenAPI client type for integration. ```bash npx @boundaryml/baml init \ --client-type rest/openapi --openapi-client-type $OPENAPI_CLIENT_TYPE ``` -------------------------------- ### Non-Streaming React Hook Example Source: https://docs.boundaryml.com/guide/framework-integration/react-next-js/quick-start Example of using the generated `useWriteMeAStory` hook in a React component for non-streaming BAML function calls. Ensure the hook and types are imported correctly. ```tsx 'use client' import { useWriteMeAStory } from "@/baml_client/react/hooks"; import type { Story } from "@/baml_client/types"; export function StoryForm() { const writeMeAStory = useWriteMeAStory({ stream: false }); return (
``` -------------------------------- ### Initialize BAML Project for REST/OpenAPI Source: https://docs.boundaryml.com/guide/installation-language/rest-api-other-languages Initialize a BAML project with specific client types for various languages. ```bash npx @boundaryml/baml init \ --client-type rest/openapi --openapi-client-type csharp ``` ```bash npx @boundaryml/baml init \ --client-type rest/openapi --openapi-client-type cpp-restsdk ``` ```bash npx @boundaryml/baml init \ --client-type rest/openapi --openapi-client-type go ``` ```bash npx @boundaryml/baml init \ --client-type rest/openapi --openapi-client-type java ``` ```bash npx @boundaryml/baml init \ --client-type rest/openapi --openapi-client-type php ``` ```bash npx @boundaryml/baml init \ --client-type rest/openapi --openapi-client-type ruby ``` ```bash npx @boundaryml/baml init \ --client-type rest/openapi --openapi-client-type rust ``` -------------------------------- ### Initialize BAML project with npm Source: https://docs.boundaryml.com/guide/installation-language/typescript Run this command to initialize a new BAML project structure in your existing project using npm. ```bash npx baml-cli init ``` -------------------------------- ### Initialize BAML project with bun Source: https://docs.boundaryml.com/guide/installation-language/typescript Run this command to initialize a new BAML project structure in your existing project using bun. ```bash bun baml-cli init ``` -------------------------------- ### Dynamic Class Definition Source: https://docs.boundaryml.com/ref/baml_client/type-builder Example of a class definition with the @@dynamic attribute. ```baml class User { name string age int @@dynamic // Allow adding more properties } ``` -------------------------------- ### Baml Client Configuration in Go Source: https://docs.boundaryml.com/ref/baml_client/with-options Go requires options to be passed individually to each function call, as it does not support client pre-configuration with `with_options`. This example shows how to configure options for reuse and pass them explicitly. ```go package main import ( "context" "fmt" b "example.com/myproject/baml_client" ) func run() { ctx := context.Background() // Configure options for reuse collector, err := b.NewCollector("my-collector") if err != nil { panic(err) } clientRegistry, err := b.NewClientRegistry() if err != nil { panic(err) } err = clientRegistry.SetPrimary("openai/gpt-5-mini") if err != nil { panic(err) } // All calls must explicitly pass options res, err := b.ExtractResume(ctx, "...", nil, b.WithCollector(collector), b.WithClientRegistry(clientRegistry)) if err != nil { panic(err) } invoice, err := b.ExtractInvoice(ctx, "...", b.WithCollector(collector), b.WithClientRegistry(clientRegistry)) if err != nil { panic(err) } // Access logs from collector logs, err := collector.Logs() if err != nil { panic(err) } fmt.Printf("Logs: %+v\n", logs) } ``` -------------------------------- ### Configure Client Without Streaming Source: https://docs.boundaryml.com/ref/llm-client-providers/anthropic Example of disabling streaming for an LLM client. ```baml client MyClientWithoutStreaming { provider anthropic options { model claude-3-5-haiku-20241022 api_key env.ANTHROPIC_API_KEY max_tokens 1000 supports_streaming false } } ``` -------------------------------- ### Handling Errors in Ruby Source: https://docs.boundaryml.com/guide/baml-basics/error-handling Placeholder for future Ruby error handling examples. ```ruby # Example coming soon ``` -------------------------------- ### Configure Baml Client with Options in Ruby Source: https://docs.boundaryml.com/ref/baml_client/with-options Use `with_options` to create a client with default settings. Requires `baml_client` gem. ```ruby require 'baml_client' collector = Baml::Collector.new(name: "my-collector") client_registry = Baml::ClientRegistry.new client_registry.set_primary("openai/gpt-5-mini") my_b = Baml.Client.with_options(collector: collector, client_registry: client_registry) # All calls will use the configured options res = my_b.ExtractResume(input: "...") invoice = my_b.ExtractInvoice(input: "...") # Access configuration print(my_b.client_registry) print(collector.logs) print(collector.last.usage) ``` -------------------------------- ### Initialize BAML Project for OpenAPI (Go Client) Source: https://docs.boundaryml.com/ref/baml-cli/init When initializing for OpenAPI clients, use both `--client-type` and `--openapi-client-type` to specify the OpenAPI client generator. This example generates a Go client. ```bash baml init --client-type openapi --openapi-client-type go ``` -------------------------------- ### Install BAML dependencies Source: https://docs.boundaryml.com/guide/installation-language/ruby Add the required BAML gems to your Ruby project. ```bash bundle add baml sorbet-runtime ``` -------------------------------- ### Configure streaming support Source: https://docs.boundaryml.com/ref/llm-client-providers/open-ai Examples of disabling streaming in BAML and invoking the function from Python. ```baml client MyClientWithoutStreaming { provider openai options { model gpt-5 api_key env.OPENAI_API_KEY supports_streaming false } } function MyFunction() -> string { client MyClientWithoutStreaming prompt #"Write a short story"# } ``` ```python # This will be streamed from your python code perspective, # but under the hood it will call the non-streaming HTTP API # and then return a streamable response with a single event b.stream.MyFunction() # This will work exactly the same as before b.MyFunction() ``` -------------------------------- ### Configure BAML Client in Ruby Source: https://docs.boundaryml.com/ref/baml_client/with-options Use Baml.Client.with_options to initialize the client and pass baml_options for overrides. ```ruby require 'baml_client' # Set up default options for this client collector = Baml::Collector.new(name: "my-collector") client_registry = Baml::ClientRegistry.new client_registry.set_primary("openai/gpt-5-mini") env = {"BAML_LOG": "DEBUG", "OPENAI_API_KEY": "key-123"} # Create client with default options my_b = Baml.Client.with_options(collector: collector, client_registry: client_registry, env: env) # Uses the default options result = my_b.ExtractResume(input: "...") # Override options for a specific call other_collector = Baml::Collector.new(name: "other-collector") result2 = my_b.ExtractResume(input: "...", baml_options: { collector: other_collector }) ``` -------------------------------- ### Additional Use Cases Source: https://docs.boundaryml.com/ref/llm-client-providers/open-ai-responses-api Examples demonstrating advanced configurations for the OpenAI Responses provider. ```APIDOC ## Additional Use Cases ### Image Input Support The `openai-responses` provider supports image inputs for vision-capable models: ```baml BAML client OpenAIResponsesVision { provider openai-responses options { model "gpt-4.1" } } function AnalyzeImage(image: image|string) -> string { client OpenAIResponsesVision prompt #" {{ _.role("user") }} What is in this image? {{ image }} "# } ``` ### Advanced Reasoning Using reasoning models with high effort for complex problem solving: ```baml BAML client AdvancedReasoningClient { provider openai-responses options { model "o4-mini" reasoning { effort "high" } } } function SolveComplexProblem(problem: string) -> string { client AdvancedReasoningClient prompt #" {{ _.role("user") }} Solve this step by step: {{ problem }} "# } ``` ``` -------------------------------- ### Define BAML Checked Types Source: https://docs.boundaryml.com/guide/baml-advanced/checks-and-asserts Example of defining a BAML type with a @check attribute. ```baml type Bar = ( bar int @check(less_than_zero, {{ this < 0 }}) )[] ``` -------------------------------- ### Configure Baml Client with Options in Python Source: https://docs.boundaryml.com/ref/baml_client/with-options Use `with_options` to create a client with default settings. These defaults can be overridden when needed. Requires `baml_client` and `baml_py` imports. ```python from baml_client import b from baml_py import ClientRegistry, Collector def run(): # Configure options collector = Collector(name="my-collector") client_registry = ClientRegistry() client_registry.set_primary("openai/gpt-5-mini") # Create configured client my_b = b.with_options(collector=collector, 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) # Access logs from the collector print(collector.logs) print(collector.last) ``` -------------------------------- ### Install BAML Go Runtime Source: https://docs.boundaryml.com/guide/installation-language/go Adds the BAML Go runtime library as a dependency to your project. This allows your Go application to interact with BAML. ```bash go get github.com/boundaryml/baml ``` -------------------------------- ### Retrieve current log level in TypeScript Source: https://docs.boundaryml.com/ref/baml_client/config Get the currently active logging level. ```typescript getLogLevel(): "INFO" | "DEBUG" | "TRACE" | "WARN" | "ERROR" | "OFF"; ``` -------------------------------- ### Configure ClientRegistry across languages Source: https://docs.boundaryml.com/guide/baml-advanced/llm-client-registry Create and configure a ClientRegistry to manage multiple LLM clients and set a primary client for function execution. ```python import os from baml_py import ClientRegistry async def run(): cr = ClientRegistry() # Creates a new client cr.add_llm_client(name='MyAmazingClient', provider='openai', options={ "model": "gpt-5-mini", "temperature": 0.7, "api_key": os.environ.get('OPENAI_API_KEY') }) # Creates a client using the OpenAI Responses API cr.add_llm_client(name='MyResponsesClient', provider='openai-responses', options={ "model": "gpt-4.1", "api_key": os.environ.get('OPENAI_API_KEY') }) # Sets MyAmazingClient as the client cr.set_primary('MyAmazingClient') # ExtractResume will now use MyAmazingClient as the calling client res = await b.ExtractResume("...", { "client_registry": cr }) ``` ```typescript import { ClientRegistry } from '@boundaryml/baml' async function run() { const cr = new ClientRegistry() // Creates a new client cr.addLlmClient('MyAmazingClient', 'openai', { model: "gpt-5-mini", temperature: 0.7, api_key: process.env.OPENAI_API_KEY }) // Creates a client using the OpenAI Responses API cr.addLlmClient('MyResponsesClient', 'openai-responses', { model: "gpt-4.1", api_key: process.env.OPENAI_API_KEY }) // Sets MyAmazingClient as the client cr.setPrimary('MyAmazingClient') // ExtractResume will now use MyAmazingClient as the calling client const res = await b.ExtractResume("...", { clientRegistry: cr }) } ``` ```ruby require_relative "baml_client/client" def run cr = Baml::ClientRegistry.new # Creates a new client cr.add_llm_client( 'MyAmazingClient', 'openai', { model: 'gpt-5-mini', temperature: 0.7, api_key: ENV['OPENAI_API_KEY'] } ) # Creates a client using the OpenAI Responses API cr.add_llm_client( 'MyResponsesClient', 'openai-responses', { model: 'gpt-4.1', api_key: ENV['OPENAI_API_KEY'] } ) # Sets MyAmazingClient as the client cr.set_primary('MyAmazingClient') # ExtractResume will now use MyAmazingClient as the calling client res = Baml.Client.extract_resume(input: '...', baml_options: { client_registry: cr }) end # Call the asynchronous function run ``` ```go package main import ( "context" "fmt" "os" "github.com/boundaryml/baml" ) func main() { ctx := context.Background() // Create a client registry cr := baml.NewClientRegistry() // Creates a new client err := cr.AddLLMClient("MyAmazingClient", "openai", map[string]interface{}{ "model": "gpt-5-mini", "temperature": 0.7, "api_key": os.Getenv("OPENAI_API_KEY"), }) if err != nil { panic(fmt.Sprintf("Failed to add client: %v", err)) } // Creates a client using the OpenAI Responses API err = cr.AddLLMClient("MyResponsesClient", "openai-responses", map[string]interface{}{ "model": "gpt-4.1", "api_key": os.Getenv("OPENAI_API_KEY"), }) if err != nil { panic(fmt.Sprintf("Failed to add responses client: %v", err)) } // Sets MyAmazingClient as the client cr.SetPrimary("MyAmazingClient") // ExtractResume will now use MyAmazingClient as the calling client res, err := baml.ExtractResume(ctx, "...", b.WithClientRegistry(cr)) if err != nil { panic(fmt.Sprintf("Failed to extract resume: %v", err)) } fmt.Printf("Result: %+v\n", res) } ``` ```rust use baml::ClientRegistry; use myproject::baml_client::sync_client::B; use std::collections::HashMap; fn main() { let mut registry = ClientRegistry::new(); // Creates a new client let mut options = HashMap::new(); options.insert("model".to_string(), serde_json::json!("gpt-5-mini")); ``` -------------------------------- ### Retrieve current log level in Python Source: https://docs.boundaryml.com/ref/baml_client/config Get the currently active logging level. ```python def get_log_level() -> "INFO" | "DEBUG" | "TRACE" | "WARN" | "ERROR" | "OFF": ``` -------------------------------- ### Context and Cancellation in Go with BAML Source: https://docs.boundaryml.com/guide/installation-language/go Illustrates how to use `context.Context` with BAML Go functions for setting timeouts and handling cancellation. Demonstrates `context.WithTimeout` and `context.WithCancel`. ```go // Set timeouts ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) deferr cancel() result, err := b.ExtractResume(ctx, resume) // Handle cancellation ctx, cancel := context.WithCancel(context.Background()) go func() { time.Sleep(5 * time.Second) cancel() // Cancel the request after 5 seconds }() result, err := b.ExtractResume(ctx, resume) if errors.Is(err, context.Canceled) { fmt.Println("Request was canceled") } ``` -------------------------------- ### Configure Go multi-stage Docker builds Source: https://docs.boundaryml.com/guide/development/deploying/docker Strategies for ensuring the libbaml-cffi native library is available in the final runtime image. ```dockerfile # Build stage FROM golang:1.21 AS builder ENV BAML_CACHE_DIR=/baml-cache WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN go install github.com/boundaryml/baml/baml-cli@latest RUN baml-cli generate --from baml_src RUN go build -o myapp # Runtime stage FROM debian:bookworm-slim ENV BAML_CACHE_DIR=/baml-cache COPY --from=builder /app/myapp /myapp COPY --from=builder /baml-cache /baml-cache CMD ["/myapp"] ``` ```dockerfile # Build stage FROM golang:1.21 AS builder WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN go install github.com/boundaryml/baml/baml-cli@latest RUN baml-cli generate --from baml_src RUN go build -o myapp # Runtime stage FROM debian:bookworm-slim WORKDIR /app COPY --from=builder /app/myapp /myapp COPY --from=builder /go/bin/baml-cli /usr/local/bin/baml-cli # Download libbaml-cffi into the runtime image RUN baml-cli --version CMD ["/myapp"] ``` -------------------------------- ### Initialize BAML Project Source: https://docs.boundaryml.com/guide/framework-integration/react-next-js/quick-start Run the BAML CLI to initialize a new BAML project within your Next.js application. This creates a `baml_src` directory. ```bash npx baml-cli init ``` ```bash pnpm exec baml-cli init ``` ```bash yarn baml-cli init ``` ```bash bun baml-cli init ``` ```bash deno run --unstable-sloppy-imports -A npm:@boundaryml/baml/baml-cli init ``` -------------------------------- ### Configure High Reasoning Effort Source: https://docs.boundaryml.com/ref/llm-client-providers/open-ai-responses-api Example of setting the reasoning effort parameter for the openai-responses provider. ```BAML client HighReasoningClient { provider openai-responses options { model "o4-mini" reasoning { effort "high" } } } ``` -------------------------------- ### Hook Output Type Examples Source: https://docs.boundaryml.com/ref/baml_client/react-next-js/hook-output Illustrates the structure of the HookOutput object for both streaming and non-streaming configurations. ```typescript // Streaming configuration const streamingResult: HookOutput<'TestAws', { stream: true }> = { data: "Any response", finalData: "Final response", streamData: "Streaming response...", error: undefined, isError: false, isLoading: true, isSuccess: false, isStreaming: true, isPending: false, status: 'streaming', mutate: async () => new ReadableStream(), reset: () => void } // Non-streaming configuration const nonStreamingResult: HookOutput<'TestAws', { stream: false }> = { data: "Final response", finalData: "Final response", error: undefined, isError: false, isLoading: false, isSuccess: true, isPending: false, status: 'success', mutate: async () => "Final response", reset: () => void } ``` -------------------------------- ### TypeBuilder Method Examples Source: https://docs.boundaryml.com/guide/baml-advanced/dynamic-types Common methods available on the TypeBuilder instance for defining field types. ```rust tb.string() ``` ```rust tb.int() ``` ```rust tb.float() ``` ```rust tb.bool() ``` ```rust tb.literal_string("hello") ``` ```rust tb.literal_int(123) ``` ```rust tb.literal_bool(true) ``` ```rust tb.list(tb.string()) ``` ```rust tb.union([tb.string(), tb.int()]) ``` ```rust tb.map(tb.string(), tb.int()) ``` ```rust tb.add_class("User") ``` ```rust tb.add_enum("Category") ``` ```rust tb.MyClass.type() ```