{"record":{"id":"b1cba9690c2021a3","repo":"BoundaryML/baml","slug":"encoding-map-value-w","errorCode":null,"errorMessage":"encoding map value: %w","messagePattern":"encoding map value: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_go/baml_go/serde/encode.go","lineNumber":220,"sourceCode":"\t\t\treturn nil, fmt.Errorf(\"encoding list element %d: %w\", i, err)\n\t\t}\n\t\tvalues[i] = elemOffset\n\t}\n\n\treturn &cffi.HostListValue{\n\t\tValues: values,\n\t}, nil\n}\n\n// encodeMap now accepts and passes TypeMap\nfunc encodeMap(mapValue reflect.Value) (*cffi.HostMapValue, error) {\n\n\tentries := make([]*cffi.HostMapEntry, 0, mapValue.Len())\n\tfor _, key := range mapValue.MapKeys() {\n\t\tvalue := mapValue.MapIndex(key)\n\t\tvalueHolder, err := encodeValue(value.Interface())\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"encoding map value: %w\", err)\n\t\t}\n\n\t\tswitch key.Kind() {\n\t\tcase reflect.String:\n\t\t\t// Go doesn't have enums, so we can't detect them here\n\t\t\tentries = append(entries, &cffi.HostMapEntry{\n\t\t\t\tKey:   &cffi.HostMapEntry_StringKey{StringKey: key.String()},\n\t\t\t\tValue: valueHolder,\n\t\t\t})\n\t\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\n\t\t\tentries = append(entries, &cffi.HostMapEntry{\n\t\t\t\tKey:   &cffi.HostMapEntry_IntKey{IntKey: key.Int()},\n\t\t\t\tValue: valueHolder,\n\t\t\t})\n\t\tcase reflect.Bool:\n\t\t\tentries = append(entries, &cffi.HostMapEntry{\n\t\t\t\tKey:   &cffi.HostMapEntry_BoolKey{BoolKey: key.Bool()},\n\t\t\t\tValue: valueHolder,","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/baml_go/serde/encode.go#L202-L238","documentation":"Contextual wrapper added by encodeMap when the value stored under some key fails encodeValue. The wrapped cause names the real problem (unsupported type, failed serializer, etc.); the map iteration order determines which key surfaces first, so failures may appear nondeterministic across runs.","triggerScenarios":"String-keyed map inputs (or dynamic class fields) whose values include unencodable types — nested raw structs, Checked/StreamState values, or internal objects with failing Encode().","commonSituations":"map[string]any payloads assembled from multiple sources; maps mixing valid primitives with one bad custom type; JSON round-tripped data containing unexpected types.","solutions":["Inspect the wrapped cause to identify the failing value type","Iterate and type-check map values before the call to find bad entries deterministically","Convert or remove unsupported values; add serializers for custom types","Flatten nested custom structs into maps of primitives"],"exampleFix":"// before\nm := map[string]any{\"ok\": 1, \"bad\": make(chan int)}\nb.Fn(ctx, m)\n// after\nm := map[string]any{\"ok\": 1}\nb.Fn(ctx, m)","handlingStrategy":"validation","validationCode":"func checkMapValues(m map[string]any) error { for k, v := range m { if err := checkEncodable(v); err != nil { return fmt.Errorf(\"key %q: %w\", k, err) } }; return nil }","typeGuard":"func isEncodable(v any) bool { switch v.(type) { case string, int, int64, float64, bool, nil: return true }; k := reflect.ValueOf(v).Kind(); return k == reflect.Slice || k == reflect.Map }","tryCatchPattern":"if err := b.Fn(ctx, m); err != nil {\n  if strings.Contains(err.Error(), \"encoding map value\") { /* iterate keys and validate each value */ }\n}","preventionTips":["Validate every map value before baml calls","Avoid mixed-type dynamic maps; build payloads from typed structs","Convert custom types to serializers or primitives at construction time"],"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"}