{"record":{"id":"e95ad947fba49b67","repo":"BoundaryML/baml","slug":"map-key-type-must-be-string-got-s","errorCode":null,"errorMessage":"map key type must be string, got %s","messagePattern":"map key type must be string, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_go/baml_go/serde/encode.go","lineNumber":175,"sourceCode":"\t\t\tValue: &cffi.HostValue_BoolValue{\n\t\t\t\tBoolValue: rv.Bool(),\n\t\t\t},\n\t\t}, nil\n\n\tcase reflect.Slice, reflect.Array:\n\t\tencoded, err := encodeList(rv)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"encoding list: %w\", err)\n\t\t}\n\t\treturn &cffi.HostValue{\n\t\t\tValue: &cffi.HostValue_ListValue{\n\t\t\t\tListValue: encoded,\n\t\t\t},\n\t\t}, nil\n\n\tcase reflect.Map:\n\t\tif rv.Type().Key().Kind() != reflect.String {\n\t\t\treturn nil, fmt.Errorf(\"map key type must be string, got %s\", rv.Type().Key().Kind())\n\t\t}\n\n\t\tencoded, err := encodeMap(rv)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"encoding map: %w\", err)\n\t\t}\n\t\treturn &cffi.HostValue{\n\t\t\tValue: &cffi.HostValue_MapValue{\n\t\t\t\tMapValue: encoded,\n\t\t\t},\n\t\t}, nil\n\n\tdefault:\n\t\t// Use originalValue's type for the error message as it's more accurate to the input\n\t\treturn nil, fmt.Errorf(\"unsupported type for BAML encoding: %T (Kind: %s)\", originalValue, rv.Kind())\n\t}\n}\n","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/baml_go/serde/encode.go#L157-L193","documentation":"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.","triggerScenarios":"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.","commonSituations":"Using map[int]string lookup tables; converting JSON objects into map[any]any; legacy code with numeric-keyed dicts.","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"],"exampleFix":"// before\nm := map[int]string{1: \"a\"}\nb.Fn(ctx, m)\n// after\nm := map[string]string{\"1\": \"a\"}\nb.Fn(ctx, m)","handlingStrategy":"validation","validationCode":"func stringKeys(m any) bool { rv := reflect.ValueOf(m); return rv.Kind() == reflect.Map && rv.Type().Key().Kind() == reflect.String }","typeGuard":"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 }","tryCatchPattern":"if err := b.Fn(ctx, m); err != nil {\n  if strings.Contains(err.Error(), \"map key type must be string\") { /* convert keys and retry */ }\n}","preventionTips":["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"],"tags":["go","map","serialization","baml"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}