Think of BAML functions as type-safe wrappers around LLM API calls. When you write a BAML function, the BAML compiler generates real code (Python, TypeScript, Ruby, Go, etc.) that:
Here’s a complete example showing all the key parts:
BAML
class Resume { name string education Education[] @description("Extract in the same order listed") skills string[] @description("Only include programming languages")}class Education { school string degree string year int}function ExtractResume(resume_text: string) -> Resume { client GPT4o prompt #" Parse the following resume and return a structured representation. Resume: --- {{ resume_text }} --- {# special macro to print the output schema + instructions #} {{ ctx.output_format }} JSON: "#}
A BAML function consists of:
Function signature: Name, input parameters, and return type
Client declaration: Which LLM provider and model to use
Prompt template: Using Jinja syntax to construct the prompt
BAML generates a baml_client in your target language. Here’s how to call the function:
Python
TypeScript
Ruby
Go
from baml_client import bfrom baml_client.types import Resumedef main(): resume_text = """John Doe Python, Rust, C++ University of California, Berkeley B.S. Computer Science, 2020 """ # Call the function - returns a validated Resume object resume = b.ExtractResume(resume_text) # Fully type-checked! assert isinstance(resume, Resume) print(f"Name: {resume.name}") print(f"Skills: {', '.join(resume.skills)}")
import b from 'baml_client'import { Resume } from 'baml_client/types'async function main() { const resumeText = `John Doe Python, Rust, C++ University of California, Berkeley B.S. Computer Science, 2020` // Call the function - returns a validated Resume object const resume = await b.ExtractResume(resumeText) // Fully type-checked! console.log(`Name: ${resume.name}`) console.log(`Skills: ${resume.skills.join(', ')}`)}
Every BAML function automatically gets a streaming variant:
stream = b.stream.ExtractResume(resume_text)# Get partial results as they arrivefor partial in stream: print(f"Parsed {len(partial.skills or [])} skills so far...")# Get the final validated resultfinal = stream.get_final_response()