{"record":{"id":"314834dd2b0094ce","repo":"BoundaryML/baml","slug":"encoding-map-w","errorCode":null,"errorMessage":"encoding map: %w","messagePattern":"encoding map: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_go/baml_go/serde/encode.go","lineNumber":180,"sourceCode":"\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\n// --- Encoding helpers for specific types ---\n\n// encodeList now accepts and passes TypeMap\nfunc encodeList(value reflect.Value) (*cffi.HostListValue, error) {\n\tvalues := make([]*cffi.HostValue, value.Len())","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/baml_go/serde/encode.go#L162-L198","documentation":"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.","triggerScenarios":"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.).","commonSituations":"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.","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"],"exampleFix":"// before\nm := map[string]any{\"x\": json.Number(\"1\")}\nb.Fn(ctx, m)\n// after\nm := map[string]any{\"x\": 1}\nb.Fn(ctx, m)","handlingStrategy":"validation","validationCode":"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 }","typeGuard":"func isEncodable(v any) bool {\n  switch v.(type) { case string, int, int64, float64, bool, nil: return true }\n  rv := reflect.ValueOf(v)\n  return rv.Kind() == reflect.Slice || rv.Kind() == reflect.Map\n}","tryCatchPattern":"if err := b.Fn(ctx, m); err != nil {\n  if strings.Contains(err.Error(), \"encoding map value\") { /* log wrapped cause, sanitize map */ }\n}","preventionTips":["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"],"tags":["go","map","serialization","baml"],"backgroundTag":"invalid-argument-value","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"}