BoundaryML/baml · error
failed to encode kwargs
Error message
failed to encode kwargs: %w
What it means
Wraps failures from serde.EncodeMapEntries when BamlRuntime.NewCollector encodes the 'name' kwarg (rawobjects_constructors.go:17). The collector constructor arguments could not be serialized into the CFFI form required by the native runtime.
Solutions
- Pass a non-empty, plain-string collector name.
- Verify Go client and native runtime versions match.
- Check the wrapped serde error for the failing map entry.
Example fix
// before
if name == "" { name = "collector" }
collector, err := rt.NewCollector(name)
// after
if strings.TrimSpace(name) == "" {
return nil, errors.New("collector name must be non-empty")
}
collector, err := rt.NewCollector(name) Defensive patterns
Strategy: validation
Validate before calling
if strings.TrimSpace(name) == "" { return errors.New("collector name must be a non-empty string") } Try / catch
collector, err := rt.NewCollector(name)
if err != nil {
return fmt.Errorf("failed to create collector: %w", err)
} Prevention
- Pass a plain, non-empty string name.
- Keep the baml Go module and native runtime versions in sync.
- Inspect the wrapped serde error to find the offending kwarg.
When it happens
Trigger: BamlRuntime.NewCollector(name) where serde.EncodeMapEntries fails on the kwargs map — e.g. empty/invalid name value or an encoder version mismatch.
Common situations: Passing an empty string name where the runtime requires a value; skewed serde/CFFI encoding between Go client and native runtime.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- encoding client options
- encoding client registry
- encoding collector
- encoding env vars
- encoding function arguments
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/5b219fbf67944d02.
Report an issue: GitHub.
Appendix: source
Thrown at engine/language_client_go/pkg/rawobjects_constructors.go:17
package baml
import (
"fmt"
"github.com/boundaryml/baml/engine/language_client_go/baml_go/raw_objects"
"github.com/boundaryml/baml/engine/language_client_go/baml_go/serde"
"github.com/boundaryml/baml/engine/language_client_go/pkg/cffi"
)
// / Construct Collector
func (r *BamlRuntime) NewCollector(name string) (Collector, error) {
kwargs, err := serde.EncodeMapEntries(map[string]any{
"name": name,
}, "collector constructor args")
if err != nil {
return nil, fmt.Errorf("failed to encode kwargs: %w", err)
}
ptr, err := raw_objects.NewRawObject(r.runtime, cffi.BamlObjectType_OBJECT_COLLECTOR, kwargs)
if err != nil {
return nil, fmt.Errorf("failed to create collector: %w", err)
}
as_collector, ok := ptr.(*collector)
if !ok {
return nil, fmt.Errorf("unexpected type for collector creation: %T", ptr)
}
return as_collector, nil
}
func (r *BamlRuntime) newMediaFromUrl(mediaType MediaType, url string, mimeType *string) (media, error) {
kwargs, err := serde.EncodeMapEntries(map[string]any{
"mime_type": mimeType,View on GitHub (pinned to bd85ce9dee)