Any Language (OpenAPI)

This feature was added in: v0.55.0.

OpenAPI support is currently a preview feature. Please provide feedback either in Discord or on GitHub so that we can stabilize the feature and keep you updated!

BAML allows you to expose your BAML functions as RESTful APIs:

OpenAPI diagram

We also make it easy to generate an OpenAPI client based on your BAML functions, so that you can get a client library for free.

1

Install the BAML developer tools

  • Install the VSCode extension (marketplace) for syntax highlighting, real-time prompt previews, and an interactive testing playground.

  • Install npx and java, which you’ll need to run BAML and openapi-generator respectively

    $brew install npm openapi-generator
    ># 'npm' will install npx
    ># 'openapi-generator' will install both Java and openapi-generator-cli

    If you can’t use npx, please let us know!

    It’s currently the easiest way to use BAML and openapi-generator, but depending on your needs, we can also provide a static binary for using BAML.

2

Create a new BAML project

This will give you some starter BAML code in a baml_src directory, and also set up generator.baml based on --openapi-client-type, so that BAML will:

  • compile your BAML functions into an OpenAPI specification, and
  • configure on_generate to generate an OpenAPI client in the language of your choosing

$npx @boundaryml/baml init \
> --client-type rest/openapi --openapi-client-type csharp
3

Start the development server

This will do four things:

  • serve your BAML functions over a RESTful interface on localhost:2024
  • generate an OpenAPI schema in baml_client/openapi.yaml
  • run openapi-generator -g $OPENAPI_CLIENT_TYPE in baml_client to generate an OpenAPI client for you to use
  • re-run the above steps whenever you modify your .baml files

BAML-over-HTTP is currently a preview feature. Please provide feedback either in Discord or on GitHub so that we can stabilize the feature and keep you updated!

$npx @boundaryml/baml dev --preview
4

Use your OpenAPI client

openapi-generator will generate a README with instructions for installing and using your client; we’ve included snippets for some of the most popular languages below. Check out baml-examples for example projects with instructions for running them.

We’ve tested the below listed OpenAPI clients, but not all of them. If you run into issues with any of the OpenAPI clients, please let us know, either in Discord or by commenting on GitHub so that we can either help you out or fix it!

Run this with go run main.go:

main.go
1package main
2
3import (
4 "context"
5 "fmt"
6 "log"
7 baml "my-golang-app/baml_client"
8)
9
10func main() {
11 cfg := baml.NewConfiguration()
12 b := baml.NewAPIClient(cfg).DefaultAPI
13 extractResumeRequest := baml.ExtractResumeRequest{
14 Resume: "Ada Lovelace (@gmail.com) was an English mathematician and writer",
15 }
16 resp, r, err := b.ExtractResume(context.Background()).ExtractResumeRequest(extractResumeRequest).Execute()
17 if err != nil {
18 fmt.Printf("Error when calling b.ExtractResume: %v\n", err)
19 fmt.Printf("Full HTTP response: %v\n", r)
20 return
21 }
22 log.Printf("Response from server: %v\n", resp)
23}

Read on to learn how to deploy your BAML functions.