Functions

Declare named, typed operations with expression or model-backed bodies.

Syntax

BAMLreturn-number.baml
function return_number(value: int) -> int {
  value
}
function return_number(value: int) -> int {
  value
}
PartRequiredPurpose
Function nameYesNames the operation that callers invoke.
ParametersYesDeclares the named, typed inputs; the list may be empty.
Return typeYesDefines the result expected by BAML and generated clients.
BodyYesComputes the result or declares a client and prompt.

Parameters

Parameters are named and typed. Separate multiple parameters with commas. In BAML, pass required arguments in declaration order or by name. Parameters with defaults are passed by name. Generated SDKs map those arguments to the host language’s calling conventions.

BAMLtyped-parameters.baml
function format_summary(title: string, body: string) -> string {
  title + ": " + body
}
function format_summary(title: string, body: string) -> string {
  title + ": " + body
}

Function bodies

Expression functions evaluate ordinary BAML expressions. Model-backed functions declare a client and prompt. Both forms must satisfy the declared return type.

Body kindUse it for
ExpressionDeterministic transforms, branching, and composition.
Client + promptTyped operations whose result is produced by a model.

Examples

See a function in context in Classify support tickets. For available chapters, see the book’s table of contents.