BoundaryML/baml · error
encoding map: %w
Error message
encoding map: %w
What it means
A string-keyed map being encoded as a baml input contained a value that failed to encode; the inner error is wrapped with 'encoding map: %w'. Like the list wrapper, it is context — the wrapped cause names the actual unsupported or failing value.
Source
Thrown at engine/language_client_go/baml_go/serde/encode.go:180
case reflect.Slice, reflect.Array:
encoded, err := encodeList(rv)
if err != nil {
return nil, fmt.Errorf("encoding list: %w", err)
}
return &cffi.HostValue{
Value: &cffi.HostValue_ListValue{
ListValue: encoded,
},
}, nil
case reflect.Map:
if rv.Type().Key().Kind() != reflect.String {
return nil, fmt.Errorf("map key type must be string, got %s", rv.Type().Key().Kind())
}
encoded, err := encodeMap(rv)
if err != nil {
return nil, fmt.Errorf("encoding map: %w", err)
}
return &cffi.HostValue{
Value: &cffi.HostValue_MapValue{
MapValue: encoded,
},
}, nil
default:
// Use originalValue's type for the error message as it's more accurate to the input
return nil, fmt.Errorf("unsupported type for BAML encoding: %T (Kind: %s)", originalValue, rv.Kind())
}
}
// --- Encoding helpers for specific types ---
// encodeList now accepts and passes TypeMap
func encodeList(value reflect.Value) (*cffi.HostListValue, error) {
values := make([]*cffi.HostValue, value.Len())View on GitHub (pinned to bd85ce9dee)
Solutions
- Inspect the wrapped cause to identify the failing key/value
- Normalize map values to primitives, slices, string-keyed maps, or serializable types
- Register/implement BamlSerializer for custom value types
- Sanitize dynamic (map[string]any) data before passing it
Example fix
// before
m := map[string]any{"x": json.Number("1")}
b.Fn(ctx, m)
// after
m := map[string]any{"x": 1}
b.Fn(ctx, m) Defensive patterns
Strategy: validation
Validate before calling
func sanitizeMap(m map[string]any) error { for k, v := range m { if !isEncodable(v) { return fmt.Errorf("key %q: %T", k, v) } }; return nil } Type guard
func isEncodable(v any) bool {
switch v.(type) { case string, int, int64, float64, bool, nil: return true }
rv := reflect.ValueOf(v)
return rv.Kind() == reflect.Slice || rv.Kind() == reflect.Map
} Try / catch
if err := b.Fn(ctx, m); err != nil {
if strings.Contains(err.Error(), "encoding map value") { /* log wrapped cause, sanitize map */ }
} Prevention
- Sanitize map[string]any payloads after JSON decoding (e.g. convert json.Number)
- Keep only primitives/slices/string-keyed maps as values
- Write a pre-call validation helper shared across the codebase
When it happens
Trigger: Passing a map[string]... as a baml argument where some value is unsupported (nested non-serializable struct, Checked/StreamState, internal object whose Encode fails, etc.).
Common situations: Dynamic map[string]any payloads parsed from JSON containing unexpected types (e.g. json.Number, nested structs); maps holding media objects; maps of custom classes without serializers.
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
- map key type must be string, got %s
- encoding map value: %w
- encoding internal object: %w
- encoding list: %w
- unsupported type for BAML encoding: %T (Kind: %s)
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/314834dd2b0094ce.
Report an issue: GitHub.