Back to Baml

Generator

fern/03-reference/generator.mdx

0.222.04.3 KB
Original Source

Each generator that you define in your BAML project will tell baml-cli generate to generate code for a specific target language. You can define multiple generator clauses in your BAML project, and baml-cli generate will generate code for each of them.

<Tip>If you created your project using baml-cli init, then one has already been generated for you!</Tip>

<CodeBlocks>
baml
generator target {
    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.76.2"
}
baml
generator target {
    // Generate code will be compatible with Pydantic 1.x
    output_type "python/pydantic/v1"

    // 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.76.2"

    // Optional: run formatters or other tools after generating the client code
    on_generate "black . && isort ."
}
baml
generator target {
    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.76.2"

    // The format of the generated module.
    // "esm" - Use ES modules
    // "cjs" - Use CommonJS modules (default)
    module_format "cjs"
}
baml
generator target {
    output_type "typescript/react"

    // 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.76.2"

    // The format of the generated module.
    // "esm" - Use ES modules
    // "cjs" - Use CommonJS modules (default)
    module_format "cjs"

    // Optional: run formatters or other tools after generating the client code
    on_generate "prettier . --write"
}
baml
generator target {
    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.76.2"
}
baml
generator target {
    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.205.0"

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

    // Commands to run after code generation (mandatory for proper code formatting)
    on_generate "gofmt -w . && goimports -w . && go mod tidy"
}
baml
generator target {
    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.76.2"

    // '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>