{"record":{"id":"7fa6fd69f00c979f","repo":"BoundaryML/baml","slug":"encoding-s-field-s-w","errorCode":null,"errorMessage":"encoding %s field '%s': %w","messagePattern":"encoding (.+?) field '(.+?)': %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_go/baml_go/serde/encode.go","lineNumber":258,"sourceCode":"\t\tdefault:\n\t\t\treturn nil, fmt.Errorf(\"unsupported map key type: %s\", key.Kind())\n\t\t}\n\t}\n\n\treturn &cffi.HostMapValue{\n\t\tEntries: entries,\n\t}, nil\n}\n\n// Helper function to encode map entries into a vector offset\nfunc EncodeMapEntries(fields map[string]any, context string) ([]*cffi.HostMapEntry, error) {\n\tentries := make([]*cffi.HostMapEntry, 0, len(fields))\n\t// Build entries (order doesn't strictly matter, but need to build bottom-up)\n\tfor k, v := range fields {\n\t\tkey := k\n\t\tvalueHolder, err := encodeValue(v)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"encoding %s field '%s': %w\", context, k, err)\n\t\t}\n\n\t\tentries = append(entries, &cffi.HostMapEntry{\n\t\t\tKey:   &cffi.HostMapEntry_StringKey{StringKey: key},\n\t\t\tValue: valueHolder,\n\t\t})\n\t}\n\n\treturn entries, nil\n}\n\nfunc EncodeValue(value any) (*cffi.HostValue, error) {\n\treturn encodeValue(value)\n}\n\nfunc EncodeEnvVar(fields map[string]string) ([]*cffi.HostEnvVar, error) {\n\tif len(fields) == 0 || fields == nil {\n\t\treturn nil, nil","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/baml_go/serde/encode.go#L240-L276","documentation":"EncodeMapEntries is the public entry point that encodes a map of named fields into HostMapEntries for the BAML FFI layer. When encoding any single value fails, the error is wrapped with the context string and field name so developers know exactly which field was the problem. It is used by CallMethod, EncodeClass, client registry encoding, and media construction.","triggerScenarios":"Any nested value inside the fields map fails encodeValue: e.g. passing a map with an unsupported key type (error 1300), an unsupported value kind, or a failing nested EncodeClass. Occurs via baml.CallMethod, class encoding, encodeClientRegistry, NewCollector, or newMediaFromUrl/newMediaFromBase64.","commonSituations":"A field value is of a Go type the serde layer can't encode (chan, func, complex, cyclic structure); a media URL/base64 helper receives data that fails downstream encoding; a client registry entry contains an unsupported type.","solutions":["Read the wrapped inner error and the field name reported ('encoding <ctx> field '<k>') to locate the offending value.","Replace the offending field value with a supported type (string, number, bool, slice, map[string]..., or encodable class).","For media, verify the URL/base64 input is valid before calling the helper.","Encode fields individually with EncodeMapEntries per-field during debugging to isolate the failing value."],"exampleFix":"// before\nfields := map[string]any{\"cb\": make(chan int)}\nentries, err := serde.EncodeMapEntries(\"MyClass\", fields) // fails on 'cb'\n\n// after\nfields := map[string]any{\"cbStatus\": \"active\"} // encode supported representation\nentries, err := serde.EncodeMapEntries(\"MyClass\", fields)","handlingStrategy":"try-catch","validationCode":"func validateEncodable(v any, depth int) error {\n\tif depth > 10 {\n\t\treturn fmt.Errorf(\"too deep\")\n\t}\n\trv := reflect.ValueOf(v)\n\tswitch rv.Kind() {\n\tcase reflect.Chan, reflect.Func, reflect.Complex64, reflect.Complex128, reflect.UnsafePointer:\n\t\treturn fmt.Errorf(\"unsupported kind %s\", rv.Kind())\n\t}\n\treturn nil\n}","typeGuard":"func isEncodable(v any) bool {\n\tswitch reflect.ValueOf(v).Kind() {\n\tcase reflect.Chan, reflect.Func, reflect.Complex64, reflect.Complex128, reflect.UnsafePointer:\n\t\treturn false\n\t}\n\treturn true\n}","tryCatchPattern":"valueHolder, err := encodeValue(v)\nif err != nil {\n\tvar encErr *serde.EncodeError\n\tif errors.As(err, &encErr) {\n\t\tlog.Printf(\"field %q failed: %v\", fieldName, err)\n\t}\n\treturn err\n}","preventionTips":["Keep BAML parameter types to JSON-like shapes","Inspect the field name in the wrapped message before debugging blind","Test encoding of complex payloads in unit tests before runtime calls"],"tags":["go","serialization","ffi","error-wrapping"],"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"}