Get startedQuickstart

Python

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:

Install BAML VSCode Extension

https://marketplace.visualstudio.com/items?itemName=boundary.BAML

  • 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}

Install baml

$pip install baml-py

Add some starter code

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

$baml-cli init

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.

If you download our VSCode extension, it will automatically generate baml_client on save!

$baml-cli generate

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