Functions

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

Syntax

functions/return-number.baml
function ReturnNumber(value: int) -> int {
  value
}
function ReturnNumber(value: int) -> int {
  value
}
PartRequiredPurpose
Function nameYesNames the generated client method.
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. Callers pass them in declaration order through generated client methods.

functions/typed-parameters.baml
function Summarize(title: string, body: string, limit: int) -> string {
  title + ": " + body
}
function Summarize(title: string, body: string, limit: int) -> 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 a guided explanation, read the functions chapter.