{"record":{"id":"33836ad42137dcf0","repo":"BoundaryML/baml","slug":"encoding-method-arguments-w","errorCode":null,"errorMessage":"encoding method arguments: %w","messagePattern":"encoding method arguments: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_go/baml_go/raw_objects/utils.go","lineNumber":159,"sourceCode":"}\n\nfunc destructor(object RawPointer) error {\n\tresult, err := CallMethod(object, \"~destructor\", nil)\n\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to call destructor: %w\", err)\n\t}\n\n\tif result != nil {\n\t\treturn fmt.Errorf(\"destructor returned unexpected result: %v\", result)\n\t}\n\treturn nil\n}\n\nfunc CallMethod(object RawPointer, method_name string, kwargs map[string]any) (any, error) {\n\tcffi_kwargs, err := serde.EncodeMapEntries(kwargs, \"function arguments\")\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"encoding method arguments: %w\", err)\n\t}\n\n\targs := cffi.BamlObjectMethodInvocation{\n\t\tKwargs:     cffi_kwargs,\n\t\tObject:     EncodeRawObject(object),\n\t\tMethodName: method_name,\n\t}\n\n\tencodedArgs, err := proto.Marshal(&args)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to marshal object method arguments: %w\", err)\n\t}\n\tcEncodedArgs := (*C.char)(unsafe.Pointer(&encodedArgs[0]))\n\n\tcBuf := C.WrapCallObjectMethodFunction(object.Runtime(), cEncodedArgs, C.uintptr_t(len(encodedArgs)))\n\n\tcontent_bytes := C.GoBytes(unsafe.Pointer(cBuf.ptr), C.int32_t(cBuf.len))\n\tC.WrapFreeBuffer(cBuf) // Free the buffer after use","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/baml_go/raw_objects/utils.go#L141-L177","documentation":"This error wraps serde.EncodeMapEntries failures when encoding the kwargs map of an object method call into cffi HostMapEntry protobuf values. If any argument value cannot be serialized into the FFI wire format (unsupported type, nested structure that fails encoding), CallMethod returns this error before contacting the native side.","triggerScenarios":"Calling object methods such as TypeBuilder.AddProperty, ClassPropertyBuilder.SetType, collector methods, or b.Type(...) with kwargs containing values that serde cannot encode (e.g. unsupported Go types, channels, funcs, or invalid nested values).","commonSituations":"Passing wrong Go types to builder methods (e.g. a string where an enum/type ref is expected), passing nil maps or unsupported composite types, wrapping arbitrary structs not supported by the FFI serde layer.","solutions":["Read the wrapped %w error to find which argument key/value failed to encode","Check the method signature and pass only supported types (string, int, bool, slices, maps of supported types)","For type-builder methods pass the builder-handle objects (TypeBuilder/ClassBuilder returns), not raw strings or structs","Convert custom structs into the supported primitive/builder types before calling"],"exampleFix":"// before\ntb.Property(\"my_field\") // kwargs with unsupported struct value\n// after: pass supported types / builder handles\nfield := classBuilder.Property(\"my_field\")\nfield.SetType(b.NewTypeBuilder(rt).Type(\"string\"))","handlingStrategy":"validation","validationCode":"// only pass FFI-encodable kwargs: string, int, float, bool, nil, slices/maps of these, or BAML builder objects\nfor k, v := range kwargs {\n    switch v.(type) {\n    case nil, string, int, int64, float64, bool, []any, map[string]any, b.TypeBuilder, b.ClassBuilder:\n    default:\n        return fmt.Errorf(\"kwarg %q has unsupported type %T\", k, v)\n    }\n}","typeGuard":null,"tryCatchPattern":"res, err := builder.AddProperty(\"name\")\nif err != nil {\n    if strings.Contains(err.Error(), \"encoding method arguments\") {\n        return fmt.Errorf(\"unsupported argument type: %w\", err)\n    }\n    return err\n}","preventionTips":["Check method signatures and pass only documented types","Never pass channels, funcs, or arbitrary structs as kwargs","Use builder handle objects (returned by constructors) rather than raw identifiers where required"],"tags":["go","ffi","serialization","arguments"],"backgroundTag":"invalid-argument-format","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"}