(Vaibhav Gupta & team, 2024) is a simple prompting language for building reliable AI workflows and agents by BoundaryML. GitHub - BoundaryML/baml: The AI framework that adds the engineering to prompt engineering. Available for Python, typescript, golang, java etc.,

BAML makes prompt engineering easy by turning it into schema engineering — where you mostly focus on the models of your prompt — to get more reliable outputs.

New syntax can be incredible at expressing new ideas. Plus the idea of maintaining hundreds of f-strings for prompts kind of disgusts us 🤮. Strings are bad for maintainable codebases. We prefer structured strings.

The goal of BAML is to give you the expressiveness of English, but the structure of code.

Half the error rate, just by changing the way the schema is expressed to the LLM ! I think the lesson learned here is that:

  1. Easier to read for humans usually means easier to read for LLMs as well
  2. JSON is not easy to read for humans — X/appenz

The new BAML adapter (which subclasses the existing JSON adapter) avoids using JSON schema for formatting the data model in the prompt. Instead, it uses the format popularized by BAML, and it instantly improves the quality of structured outputs. — X/tech_optimist Using the BAMLAdapter in dspy is incredibly simple - just pass it as a parameter to your LM, and the adapter takes care of the rest.

import dspy
from pydantic import BaseModel, Field
from typing import Literal
from dspy.adapters.baml_adapter import BAMLAdapter
 
# 1. Define Pydantic models
class PatientAddress(BaseModel):
    street: str
    city: str
    country: Literal["US", "CA"]
 
class PatientDetails(BaseModel):
    name: str = Field(description="Full name of the patient.")
    age: int
    address: PatientAddress | None
 
# 2. Define a signature using the Pydantic model as an output field
class ExtractPatientInfo(dspy.Signature):
    '''Extract patient information from the clinical note.'''
    clinical_note: str = dspy.InputField()
    patient_info: PatientDetails = dspy.OutputField()
 
# 3. Configure dspy to use the custom adapter
llm = dspy.OpenAI(model="gpt-4.1-mini")
dspy.configure(lm=llm, adapter=BAMLAdapter())
 
# 4. Run your program
extractor = dspy.Predict(ExtractPatientInfo)
note = "John Doe, 45 years old, lives at 123 Main St, Anytown. Resident of the US."
result = extractor(clinical_note=note)
print(result.patient_info)
 
# Expected output:
# PatientDetails(
#    name='John Doe',
#    age=45,
#    address=PatientAddress(street='123 Main St', city='Anytown', country='US'))
 

References

Vaibhav Gupta, A. V., & team, B. M. (2024). BAML. https://github.com/boundaryml/baml