> Version notice: This site documents BAML v0, the legacy DSL. BAML v1 is a separate, fully featured programming language in public beta. If the user intends to use BAML v1, ignore this site's content and use the BAML v1 documentation instead. If the intended version is unclear, clarify it before using this page. > For clean Markdown of any page, append .md to the page URL. For the complete documentation index, see https://docs.boundaryml.com/llms.txt. For MCP access, connect to https://docs.boundaryml.com/_mcp/server. Functions in BAML define the contract between your application and AI models, providing type-safe interfaces for AI operations. ## Overview A BAML function consists of: * Input parameters with explicit types * A return type specification * An [LLM client](client-llm) * A prompt (as a [block string](general-baml-syntax/string#block-strings)) ```baml function FunctionName(param: Type) -> ReturnType { client ModelName prompt #" Template content "# } ``` ## Function Declaration ### Syntax ```baml function name(parameters) -> return_type { client llm_specification prompt block_string_specification } ``` ### Parameters * `name`: The function identifier (must start with a capital letter!) * `parameters`: One or more typed parameters (e.g., `text: string`, `data: CustomType`) * `return_type`: The type that the function guarantees to return (e.g., `string | MyType`) * `llm_specification`: The LLM to use (e.g., `"openai/gpt-5-mini"`, `GPT5`, `Claude4`) * `block_string_specification`: The prompt template using Jinja syntax ## Type System Functions leverage BAML's strong type system, supporting: ### Built-in Types * `string`: Text data * `int`: Integer numbers * `float`: Decimal numbers * `bool`: True/false values * `array`: Denoted with `[]` suffix (e.g., `string[]`) * `map`: Key-value pairs (e.g., `map`) * `literal`: Specific values (e.g., `"red" | "green" | "blue"`) * [See all](types) ### Custom Types Custom types can be defined using class declarations: ```baml class CustomType { field1 string field2 int nested NestedType } function ProcessCustomType(data: CustomType) -> ResultType { // ... } ``` ## Prompt Templates ### Jinja Syntax BAML uses Jinja templating for dynamic prompt generation: ```baml prompt #" Input data: {{ input_data }} {% if condition %} Conditional content {% endif %} {{ ctx.output_format }} "# ``` ### Special Variables * `ctx.output_format`: Automatically generates format instructions based on return type * `ctx.client`: Selected client and model name * `_.role`: Define the role of the message chunk ## Error Handling Functions automatically handle common AI model errors and provide type validation: * JSON parsing errors are automatically corrected * Type mismatches are detected and reported * Network and rate limit errors are propagated to the caller ## Usage Examples ### Basic Function ```baml function ExtractEmail(text: string) -> string { client GPT4Turbo prompt #" Extract the email address from the following text: {{ text }} {{ ctx.output_format }} "# } ``` ### Complex Types ```baml class Person { name string age int contacts Contact[] } class Contact { type "email" | "phone" value string } function ParsePerson(data: string) -> Person { client "openai/gpt-5" prompt #" {{ ctx.output_format }} {{ _.role('user') }} {{ data }} "# } ``` ## `baml_client` Integration **`Python`** ```python Python from baml_client import b from baml_client.types import Person async def process() -> Person: result = b.ParsePerson("John Doe, 30 years old...") print(result.name) # Type-safe access return result ``` **`TypeScript`** ```typescript TypeScript import { b } from 'baml-client'; import { Person } from 'baml-client/types'; async function process(): Promise { const result = await b.ParsePerson("John Doe, 30 years old..."); console.log(result.name); // Type-safe access return result; } ```