https://www.boundaryml.com logo
🟡 BAML Go Generator - Compilation Errors
# questions
s
BAML Go Generator - Compilation Errors (v0.216.0) Hey team! First time BAML user here. I'm running into compilation errors with the Go code generator. The generated code has missing and unused imports that cause go build to fail. Environment: - BAML: 0.216.0 - Go: 1.24.11 - macOS: Darwin 19.6.0 (older version - happy to test on a different system if needed to rule this out) Quick summary: - Missing reflect import in type_map.go - Missing fmt import in runtime.go, functions*.go, type_builder.go - Missing type_builder package import in runtime.go - Several files have unused imports (encoding/json, types, stream_types, etc.) which Go treats as errors I was able to work around it by patching imports post-generation, but wanted to flag it. Could be a generator template issue, or possibly something specific to my environment. Happy to provide more details or test a fix if helpful!
v
Yep! We recommend adding this on_generate hook to the generator:
Copy code
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"
}
You may need to do: # gofmt comes with Go by default, but install goimports go install golang.org/x/tools/cmd/goimports@latest
s
Thank You! Works 🙏