BoundaryML/baml · error
encoding function arguments: %w
Error message
encoding function arguments: %w
What it means
BamlFunctionArguments.encode() serializes the call's keyword arguments into protobuf map entries before crossing the FFI boundary. If serde.EncodeMapEntries fails, the error is wrapped with this message, meaning one or more argument values could not be encoded into the CFFI representation.
Source
Thrown at engine/language_client_go/pkg/rawobjects_function_args.go:32
ClientRegistry *ClientRegistry
Env map[string]string
Collectors []Collector
TypeBuilder TypeBuilder
Tags map[string]string
}
func (args *BamlFunctionArguments) Encode() ([]byte, error) {
encoded, err := args.encode()
if err != nil {
return nil, err
}
return proto.Marshal(encoded)
}
func (args *BamlFunctionArguments) encode() (*cffi.HostFunctionArguments, error) {
kwargs, err := serde.EncodeMapEntries(args.Kwargs, "function arguments")
if err != nil {
return nil, fmt.Errorf("encoding function arguments: %w", err)
}
var clientRegistry *cffi.HostClientRegistry
if args.ClientRegistry != nil {
clientRegistry, err = encodeClientRegistry(args.ClientRegistry)
if err != nil {
return nil, fmt.Errorf("encoding client registry: %w", err)
}
}
var env []*cffi.HostEnvVar
if args.Env != nil {
env, err = serde.EncodeEnvVar(args.Env)
if err != nil {
return nil, fmt.Errorf("encoding env vars: %w", err)
}
}
View on GitHub (pinned to bd85ce9dee)
Solutions
- Ensure function arguments match the types declared in the .baml function signature
- Convert custom Go structs into BAML-compatible classes via a TypeBuilder or encode to map[string]any with string keys
- Simplify the argument (log/marshal it to JSON first) to find which entry fails encoding
- Update BAML Go bindings and runtime to matching versions
Example fix
// before
kwargs := baml.NewKwargs(map[string]any{"user": myCustomGoStruct{}})
res, err := runtime.CallFunction(ctx, "Extract", params, kwargs, nil)
// after
userBytes, _ := json.Marshal(myCustomGoStruct{})
var userMap map[string]any
json.Unmarshal(userBytes, &userMap)
kwargs := baml.NewKwargs(map[string]any{"user": userMap})
res, err := runtime.CallFunction(ctx, "Extract", params, kwargs, nil) Defensive patterns
Strategy: validation
Validate before calling
func validKwargs(kwargs map[string]any) bool {
for k, v := range kwargs {
if k == "" || v == nil { return false }
switch v.(type) {
case string, int, int64, float64, bool, []any, map[string]any:
default: return false
}
}
return true
} Try / catch
res, err := runtime.CallFunction(ctx, "Fn", params, kwargs, nil)
if err != nil && strings.Contains(err.Error(), "encoding function arguments") {
return fmt.Errorf("check kwargs types match .baml signature: %w", err)
} Prevention
- Pass only JSON-like values (string/number/bool/slice/map) as kwargs
- Convert custom Go structs to map[string]any via JSON round-trip
- Match argument names exactly to the .baml function signature
When it happens
Trigger: Calling a generated BAML function (runtime.CallFunction / generated wrapper) while passing kwargs containing values not encodable by the serde layer — e.g. unsupported nested Go types, nil maps with wrong types, or values violating the serde schema.
Common situations: Passing a custom struct where the BAML function expects a primitive or BAML class; passing a map with non-string keys; version skew so the serde encoder doesn't recognize newer runtime types.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- encoding method arguments: %w
- unsupported map key type: %s
- encoding %s field '%s': %w
- encoding client registry: %w
- encoding env vars: %w
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/e930f2f277191f7d.
Report an issue: GitHub.