BoundaryML/baml · error
map key type must be string, got %s
Error message
map key type must be string, got %s
What it means
BAML map inputs must have string keys. When encodeValue encounters a reflect.Map whose key kind is not string (e.g. map[int]..., map[float64]...), it refuses to encode and reports the offending key kind.
Source
Thrown at engine/language_client_go/baml_go/serde/encode.go:175
Value: &cffi.HostValue_BoolValue{
BoolValue: rv.Bool(),
},
}, nil
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())
}
}
View on GitHub (pinned to bd85ce9dee)
Solutions
- Convert the map to map[string]T, formatting keys with strconv.Itoa/fmt.Sprintf
- Change the struct field type to a string-keyed map
- Pre-transform data into a JSON-like string-keyed structure before calling the baml function
Example fix
// before
m := map[int]string{1: "a"}
b.Fn(ctx, m)
// after
m := map[string]string{"1": "a"}
b.Fn(ctx, m) Defensive patterns
Strategy: validation
Validate before calling
func stringKeys(m any) bool { rv := reflect.ValueOf(m); return rv.Kind() == reflect.Map && rv.Type().Key().Kind() == reflect.String } Type guard
func toStringMap(m map[int]string) map[string]string { out := map[string]string{}; for k, v := range m { out[strconv.Itoa(k)] = v }; return out } Try / catch
if err := b.Fn(ctx, m); err != nil {
if strings.Contains(err.Error(), "map key type must be string") { /* convert keys and retry */ }
} Prevention
- Standardize on map[string]T for all data passed to baml functions
- Convert numeric keys to strings at the data boundary
- Add a unit test asserting input maps are string-keyed
When it happens
Trigger: Passing a Go map with non-string key types (int, bool, struct, etc.) as a baml function argument or as a class field via EncodeMapEntries paths.
Common situations: Using map[int]string lookup tables; converting JSON objects into map[any]any; legacy code with numeric-keyed dicts.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- encoding map: %w
- 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/e95ad947fba49b67.
Report an issue: GitHub.