Skip to main content

Literal Types

Literal types constrain primitive types to specific values.

info

This feature was added in v0.61.0.

Syntax​

"specific_string"
42
true
false

Supported Literal Types​

Base TypeLiteral Example
string"bug", "feature", "red"
int1, 42, -5
booltrue, false

Examples​

String Literals​

function ClassifyIssue(description: string) -> "bug" | "enhancement" {
client "openai/gpt-4o"
prompt #"
Classify the issue based on the description:
{{ ctx.output_format }}

{{ _.role("user") }}
{{ description }}
"#
}

Mixed Literals​

function GetStatus() -> "active" | "inactive" | 0 | 1 {
client "openai/gpt-4o"
prompt #"
Return the status.
{{ ctx.output_format }}
"#
}

Literal Unions​

"str" | 1 | false

Usage in Maps​

Literal strings can be used as map keys:

map<"A" | "B" | "C", string>

Language Equivalents​

BAMLPythonTypeScriptGo
"str" | 1 | falseUnion[Literal["str"], Literal[1], Literal[False]]"str" | 1 | falseUnion3StringOrIntOrBool
"red" | "green" | "blue"Literal["red", "green", "blue"]"red" | "green" | "blue"string (with validation)

See Also​