Here’s a sample repository: https://github.com/BoundaryML/baml-examples/tree/main/python-fastapi-starter

To set up BAML in python do the following:

1

Install BAML VSCode Extension

https://marketplace.visualstudio.com/items?itemName=boundary.baml-extension

  • syntax highlighting
  • testing playground
  • prompt previews

In your VSCode User Settings, highly recommend adding this to get better autocomplete for python in general, not just BAML.

1{
2 "python.analysis.typeCheckingMode": "basic"
3}
2

Install baml

$pip install baml-py
3

Add some starter code

This will give you some starter BAML code in a baml_src directory.

$baml-cli init
4

Generate python code from .baml files

This command will help you convert .baml files to .py files. Everytime you modify your .baml files, you must re-run this command, and regenerate the baml_client folder.

Our VSCode extension automatically runs this command when you save a BAML file.

$baml-cli generate
5

Use a baml function in python!

If baml_client doesn’t exist, make sure to run the previous step!
1from baml_client.sync_client import b
2from baml_client.types import Resume
3
4def example(raw_resume: str) -> Resume:
5 # BAML's internal parser guarantees ExtractResume
6 # to be always return a Resume type
7 response = b.ExtractResume(raw_resume)
8 return response
9
10def example_stream(raw_resume: str) -> Resume:
11 stream = b.stream.ExtractResume(raw_resume)
12 for msg in stream:
13 print(msg) # This will be a PartialResume type
14
15 # This will be a Resume type
16 final = stream.get_final_response()
17
18 return final

BAML with Jupyter Notebooks

You can use the baml_client in a Jupyter notebook.

One of the common problems is making sure your code changes are picked up by the notebook without having to restart the whole kernel (and re-run all the cells)

To make sure your changes in .baml files are reflected in your notebook you must do these steps:

1

Setup the autoreload extension

cell0
1%load_ext autoreload
2%autoreload 2

This will make sure to reload imports, such as baml_client’s “b” object before every cell runs.

2

Import baml_client module in your notebook

Note it’s different from how we import in python.

cell1
1# Assuming your baml_client is inside a dir called app/
2import app.baml_client as client # you can name this "llm" or "baml" or whatever you want

Usually we import things as from baml_client import b, and we can call our functions using b, but the %autoreload notebook extension does not work well with from...import statements.

3

Call BAML functions using the module name as a prefix

cell2
1raw_resume = "Here's some resume text"
2client.b.ExtractResume(raw_resume)

Now your changes in .baml files are reflected in your notebook automatically, without needing to restart the Jupyter kernel.

If you want to keep using the from baml_client import b style, you’ll just need to re-import it everytime you regenerate the baml_client.

Pylance will complain about any schema changes you make in .baml files. You can ignore these errors. If you want it to pick up your new types, you’ll need to restart the kernel. This auto-reload approach works best if you’re only making changes to the prompts.