A function is the contract between the application and the AI model. It defines the desired input and a guaranteed output.

Here is a simple BAML function to extract a resume. Note the input is a chunk of resume_text, and the output is an actual resume class. Read prompt syntax to learn more about the prompt and what Jinja templating is.

BAML
1class Resume {
2 name string
3 education Education[] @description("Extract in the same order listed")
4 skills string[] @description("Only include programming languages")
5}
6
7class Education {
8 school string
9 degree string
10 year int
11}
12
13function ExtractResume(resume_text: string) -> Resume {
14 client GPT4Turbo
15 // The prompt uses Jinja syntax. Change the models or this text and watch the prompt preview change!
16 prompt #"
17 Parse the following resume and return a structured representation of the data in the schema below.
18
19 Resume:
20 ---
21 {{ resume_text }}
22 ---
23
24 {# special macro to print the output instructions. #}
25 {{ ctx.output_format }}
26
27 JSON:
28 "#
29}

A function signature directly translates into the same function in the language of your choice, and BAML’s fuzzy parser will handle fixing any common json mistakes LLMs make. Here’s how you call it:

1from baml_client import b
2from baml_client.types import Resume
3
4async def main():
5resume_text = """Jason Doe\nPython, Rust\nUniversity of California, Berkeley, B.S.\nin Computer Science, 2020\nAlso an expert in Tableau, SQL, and C++\n"""
6
7 # this function comes from the autogenerated "baml_client".
8 # It calls the LLM you specified and handles the parsing.
9 resume = await b.ExtractResume(resume_text)
10
11 # Fully type-checked and validated!
12 assert isinstance(resume, Resume)

Complex input types

If you have a complex input type you can import them from baml_client and use them when calling your function. Imagine we injected class Resume into a different baml function called AnalyzeResume. Here’s what the call looks like:

1from baml_client.types import Resume
2from baml_client import b
3...
4 await b.AnalyzeResume(
5 Resume(name="Mark", education=[...]))

See more at Calling functions

Checkout PromptFiddle to see various interactive BAML function examples.