Go

To set up BAML with Go do the following:

1

Install BAML VSCode/Cursor Extension

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

  • syntax highlighting
  • testing playground
  • prompt previews
2

Install BAML CLI and Initialize Project

go
$go install github.com/boundaryml/baml/go/baml-cli@latest && baml-cli init

This command will:

  1. Install the BAML CLI tool globally
  2. Create starter BAML code in a baml_src directory
  3. Set up the basic project structure
3

Install BAML Go Runtime

After initializing your project, install the Go runtime library:

go
$go get github.com/boundaryml/baml
4

Install Required Go Tools

The BAML generator uses gofmt and goimports to format the generated Go code. Install these tools:

go
$# gofmt comes with Go by default, but install goimports
>go install golang.org/x/tools/cmd/goimports@latest

These tools are required by the on_generate command in your generator configuration and ensure the generated code is properly formatted.

5

Generate the baml_client Go package from .baml files

One of the files in your baml_src directory will have a generator block. This tells BAML how to generate the baml_client directory, which will have auto-generated Go code to call your BAML functions.

Any types defined in .baml files will be converted into Go structs in the baml_client directory.

$baml-cli generate

You can modify your build process to always call baml-cli generate before building.

Makefile
1.PHONY: generate build
2
3generate:
4 baml-cli generate
5
6build: generate
7 go build ./...
8
9test: generate
10 go test ./...

See What is baml_client to learn more about how this works.

If you set up the VSCode extension, it will automatically run baml-cli generate on saving a BAML file.

6

Use a BAML function in Go!

If baml_client doesn’t exist, make sure to run the previous step!
main.go
1package main
2
3import (
4 "context"
5 "fmt"
6 "log"
7
8 b "example.com/myproject/baml_client"
9 "example.com/myproject/baml_client/types"
10)
11
12func main() {
13 ctx := context.Background()
14
15 // BAML's internal parser guarantees ExtractResume
16 // to always return a Resume type or an error
17 resume, err := b.ExtractResume(ctx, rawResume)
18 if err != nil {
19 log.Fatal(err)
20 }
21
22 fmt.Printf("Extracted resume: %+v\n", resume)
23}
24
25func exampleStream(rawResume string) (*types.Resume, error) {
26 ctx := context.Background()
27
28 stream, err := b.Stream.ExtractResume(ctx, rawResume)
29 if err != nil {
30 return nil, err
31 }
32
33 for value := range stream {
34 if value.IsError {
35 return nil, value.Error
36 }
37
38 if !value.IsFinal && value.Stream() != nil {
39 partial := *value.Stream()
40 fmt.Printf("Partial: %+v\n", partial) // This will be a partial Resume type
41 }
42
43 if value.IsFinal && value.Final() != nil {
44 final := *value.Final()
45 return &final, nil // This will be a complete Resume type
46 }
47 }
48
49 return nil, fmt.Errorf("stream ended without final response")
50}

Working with Go Modules

BAML integrates seamlessly with Go modules. Make sure your go.mod file includes the BAML dependency:

go.mod
1module example.com/myproject
2
3go 1.21
4
5require (
6 github.com/boundaryml/baml v0.203.1
7)

The generated baml_client package will use your module path, so you can import it as:

1import (
2 b "example.com/myproject/baml_client"
3 "example.com/myproject/baml_client/types"
4)

Context and Cancellation

All BAML Go functions require a context.Context as the first parameter, allowing you to:

1// Set timeouts
2ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
3defer cancel()
4
5result, err := b.ExtractResume(ctx, resume)
6
7// Handle cancellation
8ctx, cancel := context.WithCancel(context.Background())
9go func() {
10 time.Sleep(5 * time.Second)
11 cancel() // Cancel the request after 5 seconds
12}()
13
14result, err := b.ExtractResume(ctx, resume)
15if errors.Is(err, context.Canceled) {
16 fmt.Println("Request was canceled")
17}

You’re all set! Continue on to the Deployment Guides for your language to learn how to deploy your BAML code or check out the Interactive Examples to see more examples.