For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Help on Discord
HomeGuideExamplesBAML ReferencePlaygroundAgents.mdChangelog
HomeGuideExamplesBAML ReferencePlaygroundAgents.mdChangelog
    • Overview
  • baml-cli
    • init
    • generate
    • test
    • serve
    • dev
    • fmt
  • Language Reference
    • Types
    • function
    • test
    • template_string
    • client<llm>
    • class
    • enum
    • generator
  • Generated baml_client
    • with_options(..)
    • AbortSignal / Cancellation
    • Collector
    • logging / env vars
    • AsyncClient / SyncClient
    • TypeBuilder
    • ClientRegistry
    • client Option
    • OnTick
    • Multimodal
    • Image
    • Audio
    • Pdf
    • Video
  • Attributes
    • What are attributes?
    • @alias / @@alias
    • @description / @@description
    • @skip
    • @assert
    • @check
    • Jinja in Attributes
    • @@dynamic
  • LLM Client Providers
    • Overview
    • AWS Bedrock
    • Anthropic
    • Google AI: Gemini
    • Google: Vertex
    • OpenAI
    • OpenAI Responses API
    • OpenAI from Azure
    • OpenRouter
    • openai-generic
    • Microsoft Foundry (openai-generic)
    • Cerebras (openai-generic)
    • Groq (openai-generic)
    • Hugging Face (openai-generic)
    • Keywords AI (openai-generic)
    • Llama API (openai-generic)
    • Litellm (openai-generic)
    • LM Studio (openai-generic)
    • Ollama (openai-generic)
    • Vercel AI Gateway (openai-generic)
    • Tinfoil (openai-generic)
    • TogetherAI (openai-generic)
    • Unify AI (openai-generic)
    • vLLM (openai-generic)
  • LLM Client Strategies
    • Timeout Configuration
    • Retry Policy
    • Fallback
    • Round Robin
  • Prompt Syntax
    • What is jinja?
    • Jinja Filters
    • ctx.output_format
    • ctx.client
    • _.role
    • Variables
    • Conditionals
    • Loops
  • Editor Extension Settings
    • baml.cliPath
    • baml.generateCodeOnSave
    • baml.enablePlaygroundProxy
    • baml.syncExtensionToGeneratorVersion
Help on Discord
LogoLogo
On this page
  • Overview
  • Function Declaration
  • Syntax
  • Parameters
  • Type System
  • Built-in Types
  • Custom Types
  • Prompt Templates
  • Jinja Syntax
  • Special Variables
  • Error Handling
  • Usage Examples
  • Basic Function
  • Complex Types
  • baml_client Integration
Language Reference

function

Was this page helpful?
Edit this page
Previous

test

Next
Built with

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
  • A prompt (as a block string)
1function FunctionName(param: Type) -> ReturnType {
2 client ModelName
3 prompt #"
4 Template content
5 "#
6}

Function Declaration

Syntax

1function name(parameters) -> return_type {
2 client llm_specification
3 prompt block_string_specification
4}

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<string, int>)
  • literal: Specific values (e.g., "red" | "green" | "blue")
  • See all

Custom Types

Custom types can be defined using class declarations:

1class CustomType {
2 field1 string
3 field2 int
4 nested NestedType
5}
6
7function ProcessCustomType(data: CustomType) -> ResultType {
8 // ...
9}

Prompt Templates

Jinja Syntax

BAML uses Jinja templating for dynamic prompt generation:

1prompt #"
2 Input data: {{ input_data }}
3
4 {% if condition %}
5 Conditional content
6 {% endif %}
7
8 {{ ctx.output_format }}
9"#

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

1function ExtractEmail(text: string) -> string {
2 client GPT4Turbo
3 prompt #"
4 Extract the email address from the following text:
5 {{ text }}
6
7 {{ ctx.output_format }}
8 "#
9}

Complex Types

1class Person {
2 name string
3 age int
4 contacts Contact[]
5}
6
7class Contact {
8 type "email" | "phone"
9 value string
10}
11
12function ParsePerson(data: string) -> Person {
13 client "openai/gpt-5"
14 prompt #"
15 {{ ctx.output_format }}
16
17 {{ _.role('user') }}
18 {{ data }}
19 "#
20}

baml_client Integration

1from baml_client import b
2from baml_client.types import Person
3
4async def process() -> Person:
5 result = b.ParsePerson("John Doe, 30 years old...")
6 print(result.name) # Type-safe access
7 return result