BoundaryML/baml · error

failed to generate Go SDK: {error}

Error message

failed to generate Go SDK: {error}

What it means

`baml generate` wraps the Go SDK code emitter and converts any failure from it into this anyhow error at generate.rs:492. It means the Go code generator failed while producing the SDK for a `generator go` block; the inner `{error}` carries the underlying cause. The error surfaces only after the config has been validated (e.g. sdk_import_path was already checked via expect).

Source

Thrown at baml_language/crates/baml_cli/src/generate.rs:492

                        .files
                        .into_iter()
                        .map(|(path, content)| (path, content.into_bytes()))
                        .collect()
                }
                OutputType::Go => sdkgen_go::try_to_source_code_with_bytecode_and_metadata_and_options(
                    &pool,
                    &baml_bytecode,
                    &embedded_baml_toml,
                    &sdkgen_go::GoGenOptions {
                        naming_convention: generator.naming_convention,
                        sdk_import_path: generator
                            .sdk_import_path
                            .as_deref()
                            .expect("validated Go generator must have sdk_import_path"),
                        max_typed_union_arity: generator.max_typed_union_arity,
                    },
                )
                .map_err(|error| anyhow!("failed to generate Go SDK: {error}"))?
                .into_iter()
                .map(|(path, content)| (path, content.into_bytes()))
                .collect(),
                OutputType::Cpp => {
                    // The C++ emitter embeds source paths (reference
                    // comments only); the runtime payload is the bytecode.
                    let source_paths: Vec<PathBuf> = source_files
                        .iter()
                        .map(|sf| {
                            let path = sf.path(&db);
                            path.strip_prefix(&from).unwrap_or(&path).to_path_buf()
                        })
                        .collect();
                    sdkgen_cpp::to_source_code_with_bytecode_and_metadata(
                        &pool,
                        &source_paths,
                        &baml_bytecode,
                        &embedded_baml_toml,

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Read the inner `{error}` text to identify the underlying emitter failure
  2. Verify the Go generator block in baml.generators: sdk_import_path, output_dir, and module settings are correct
  3. Ensure the output directory exists and is writable
  4. Update the BAML CLI/toolchain to the latest version

Example fix

// before
generator go Client {
  output_dir "../go"
  sdk_import_path ""
}
// after
generator go Client {
  output_dir "../go/gen"
  sdk_import_path "github.com/myorg/myrepo/gen"
}
Defensive patterns

Strategy: try-catch

Validate before calling

// before generating, validate the generator config
toml::from_str::<Config>(&std::fs::read_to_string("baml.toml")?)?;
assert!(!gen.sdk_import_path.is_empty(), "sdk_import_path required for Go generator");
assert!(Path::new(&gen.output_dir).is_dir() || std::fs::create_dir_all(&gen.output_dir).is_ok());

Try / catch

match baml_cli::generate() {
    Err(e) if e.to_string().contains("failed to generate Go SDK") => {
        eprintln!("Go emitter failed: {e:#}"); // inspect chained cause
    }
    Err(e) => return Err(e),
    Ok(files) => write_files(files)?,
}

Prevention

When it happens

Trigger: Running `baml generate` with a Go generator whose emitter fails: invalid or conflicting Go module/import path settings, unreadable output directory, or an internal emitter bug while rendering Go source files.

Common situations: Misconfigured `sdk_import_path` or `output_dir` in baml.generators, filesystem permission problems, or version drift between the CLI and the Go emitter.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/864f9a5b3c5de033. Report an issue: GitHub.