Skip to main content

BAML

Basically a Made-up Language (BAML) is a simple prompting language that builds compact, optimized prompts. Instead of optimizing your prompts manually, you can define your schema in BAML and let BAML create the optimized prompts.

BAML uses type-definitions instead of JSON for imputs and outputs. Type-definitions use 60% less tokens than JSON schemas, with no loss of information.

JSON prompt
<system>As a genius expert, your task is to understand the content and provide the parsed objects in json that match the following json_schema:

{
"$defs": {
"Address": {
"properties": {
"street": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Street"},
"city": {"anyOf": [{"type": "string"}, {"type": "null"}], "description": "The city name in lowercase", "title": "City"},
"state": {"anyOf": [{"$ref": "#/$defs/State"}, {"type": "null"}], "description": "The state abbreviation from the predefined states"},
"zip_code": {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Zip Code"}
},
"required": ["street", "city", "state", "zip_code"],
"title": "Address",
"type": "object"
},
"Item": {
"properties": {
"name": {"title": "Name", "type": "string"},
"quantity": {"title": "Quantity", "type": "integer"}
},
"required": ["name", "quantity"],
"title": "Item",
"type": "object"
},
"State": {
"enum": ["WASHINGTON", "CALIFORNIA", "OREGON"],
"title": "State",
"type": "string"
}
},
"properties": {
"order_id": {"title": "Order Id", "type": "string"},
"total_price": {"anyOf": [{"type": "integer"}, {"type": "null"}], "description": "The total price. Don't include shipping costs.", "title": "Total Price"},
"items": {"description": "purchased_items", "items": {"$ref": "#/$defs/Item"}, "title": "Items", "type": "array"},
"shipping_address": {"anyOf": [{"$ref": "#/$defs/Address"}, {"type": "null"}]}
},
"required": ["order_id", "total_price", "items", "shipping_address"],
"title": "OrderInfo3",
"type": "object"
}

<user> {input},
<user> Return the correct JSON response within a ```json codeblock. not the JSON_SCHEMA'}]
BAML prompt
Extract the following information from the text.
{input}
---
Return the information in JSON following this schema:
{
"order_id": string,
// The total price. Don't include shipping costs.
"total_price": int | null,
"purchased_items": {
"name": string,
"quantity": int
}[],
"shipping_address": {
"street": string | null,
// The city name in lowercase
"city": string | null,
// The state abbreviation from the predefined states
"state": "States as string" | null,
"zip_code": string | null
} | null
}

Use these US States only:
States
---
WASHINGTON
OREGON
CALIFORNIA

JSON:
The JSON prompt is ~420 tokens and the BAML prompt is ~140 tokens.

Under the hood, BAML does prompt engineering using the type-definitions, sends the prompts to the LLM, and uses its underlying code to implement type-safe outputs and retries.

Interestingly, BAML manages to beat OpenAI's structured outputs (which implements constrained decoding using Guidance) in the Berkeley function-calling benchmark (BFCL).