Back to Baml

What is baml_client?

fern/01-guide/what-is-baml_client.mdx

0.222.03.8 KB
Original Source

baml_client is the code that gets generated from your BAML files that transforms your BAML prompts into the same equivalent function in your language, with validated type-safe outputs.

python
from baml_client import b
resume_info = b.ExtractResume("....some text...")

This has all the boilerplate to:

  1. call the LLM endpoint with the right parameters,
  2. parse the output,
  3. fix broken JSON (if any)
  4. return the result in a nice typed object.
  5. handle errors

In Python, your BAML types get converted to Pydantic models. In Typescript, they get converted to TypeScript types, and so on. BAML acts like a universal type system that can be used in any language.

Generating baml_client

Refer to the Installation guides for how to set this up for your language, and how to generate it.

But at a high-level, you just include a generator block in any of your BAML files.

<CodeBlocks>
baml
generator target {
    // Valid values: "python/pydantic", "typescript", "ruby/sorbet", "go"
    output_type "python/pydantic"

    // Where the generated code will be saved (relative to baml_src/)
    output_dir "../"

    // What interface you prefer to use for the generated code (sync/async)
    // Both are generated regardless of the choice, just modifies what is exported
    // at the top level
    default_client_mode "sync"

    // Version of runtime to generate code for (should match installed baml-py version)
    version "0.203.1"
}
baml
generator target {
    // Valid values: "python/pydantic", "typescript", "ruby/sorbet", "go"
    output_type "typescript"

    // Where the generated code will be saved (relative to baml_src/)
    output_dir "../"

    // What interface you prefer to use for the generated code (sync/async)
    // Both are generated regardless of the choice, just modifies what is exported
    // at the top level
    default_client_mode "async"

    // Version of runtime to generate code for (should match the package @boundaryml/baml version)
    version "0.203.1"
}
baml
generator target {
    // Valid values: "python/pydantic", "typescript", "ruby/sorbet", "go"
    output_type "go"

    // Where the generated code will be saved (relative to baml_src/)
    output_dir "../"

    // Version of runtime to generate code for (should match installed github.com/boundaryml/baml version)
    version "0.203.1"

    // Go module name for the generated client
    client_package_name "example.com/myproject"

    // Commands to run after code generation (mandatory as it cleans up the generated code)
    on_generate "gofmt -w . && goimports -w . && go mod tidy"
}
baml
generator target {
    // Valid values: "python/pydantic", "typescript", "ruby/sorbet", "go"
    output_type "ruby/sorbet"

    // Where the generated code will be saved (relative to baml_src/)
    output_dir "../"

    // Version of runtime to generate code for (should match installed `baml` package version)
    version "0.203.1"
}
baml
generator target {
    // Valid values: "python/pydantic", "typescript", "ruby/sorbet", "go", "rest/openapi"
    output_type "rest/openapi"

    // Where the generated code will be saved (relative to baml_src/)
    output_dir "../"

    // Version of runtime to generate code for (should match installed `baml` package version)
    version "0.203.1"

    // 'baml-cli generate' will run this after generating openapi.yaml, to generate your OpenAPI client
    // This command will be run from within $output_dir
    on_generate "npx @openapitools/openapi-generator-cli generate -i openapi.yaml -g OPENAPI_CLIENT_TYPE -o ."
}
</CodeBlocks>

The baml_client transforms a BAML function into the same equivalent function in your language,