{"record":{"id":"68e8ba04e63537df","repo":"deeplearning4j/deeplearning4j","slug":"illegal-type-found-for-conversion-dtype","errorCode":null,"errorMessage":"Illegal type found for conversion ${dtype}","messagePattern":"Illegal type found for conversion (.+?)","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"nd4j/samediff-import/samediff-import-api/src/main/kotlin/org/nd4j/samediff/frameworkimport/IRProtobufExtensions.kt","lineNumber":426,"sourceCode":"                    return Nd4j.create(newStringList).reshape(*shape)\n                }\n                throw IllegalArgumentException(\"Shape of ${Arrays.toString(shape)} did not match length ${stringList.size}\")\n            }\n            return Nd4j.create(stringList).reshape(*shape)\n        }\n\n        DataType.UNKNOWN -> {\n            val ret = Nd4j.empty()\n            return ret\n        }\n\n        else -> {\n            return loadDataBufferFromRawData(inputTensor)\n        }\n\n    }\n\n    throw IllegalArgumentException(\"Illegal type found for conversion ${dtype}\")\n}\n\nfun loadDataBufferFromRawData(inputTensor: TensorNamespace.TensorProto): INDArray {\n    val shape = inputTensor.dimsList.toLongArray()\n    val dtype = convertNd4jDataTypeFromNameSpaceTensorDataType(TensorNamespace.DataType.values()[inputTensor.dataType])\n    val byteArray = inputTensor.rawData.toByteArray()\n    //note: scalar can be zero\n    var totalLen = ArrayUtil.prod(*shape)\n\n\n    if(dtype == DataType.UTF8) {\n        val rawDataBuffer =  Nd4j.getDataBufferFactory().createUtf8Buffer(byteArray,byteArray.size.toLong())\n        if(shape.isNotEmpty() && totalLen > 0) {\n            if(rawDataBuffer.length() > 0) {\n                val stringInput = java.lang.String(byteArray).toString()\n                return Nd4j.create(stringInput)\n            }\n            return Nd4j.empty(dtype)","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/deeplearning4j/deeplearning4j/blob/4c22ac5fe4a8350d05d224e7f4499429f7f69c93/nd4j/samediff-import/samediff-import-api/src/main/kotlin/org/nd4j/samediff/frameworkimport/IRProtobufExtensions.kt#L408-L444","documentation":"At the end of ndarrayFromNameSpaceTensor, after the exhaustive when over TensorNamespace.DataType, the function unconditionally throws IllegalArgumentException(\"Illegal type found for conversion ${dtype}\"). In practice this line is mostly unreachable because unknown data types fall into the else branch that delegates to loadDataBufferFromRawData; hitting it means control flow fell past every branch and the tensor's data type could not be converted by any path.","triggerScenarios":"Calling ndarrayFromNameSpaceTensor (via setNameForFunctionFromDescriptors) on a TensorProto whose dataType resolves to a value with no matching when-branch and whose raw-data fallback also cannot produce a result, i.e. a data type outside the expected DL4J dtype mapping.","commonSituations":"Protobufs written by a newer exporter using a DataType enum value this nd4j version does not know; corrupted dataType fields (e.g. out-of-range int) that desync the when dispatch; custom forks that added enum values without updating the converter.","solutions":["Upgrade nd4j/samediff-import to a version whose convertNd4jDataTypeFromNameSpaceTensorDataType covers the exporter's DataType enum","Log the offending inputTensor.dataType ordinal and inspect the producing model","Re-export the model with a supported data type (e.g. cast unsupported dtypes to float32/int32 upstream)","Wrap the conversion in try/catch on IllegalArgumentException and fall back to a manual tensor decode"],"exampleFix":"// before: new enum value from newer exporter hits unhandled path\nval t = tensor { dataType = NEW_DTYPE_ORDINAL }\n// after: normalize dtype before import\nval t = tensor { dataType = FLOAT.ordinal } // or upgrade the import library","handlingStrategy":"try-catch","validationCode":"val dt = TensorNamespace.DataType.forNumber(inputTensor.dataType)\nif (dt == null || dt == TensorNamespace.DataType.UNRECOGNIZED) {\n    throw ModelImportException(\"Unknown tensor data type ordinal ${inputTensor.dataType}\")\n}","typeGuard":"fun TensorProto.hasConvertibleType(): Boolean =\n    TensorNamespace.DataType.forNumber(dataType) in setOf(\n        TensorNamespace.DataType.INT64, TensorNamespace.DataType.INT32,\n        TensorNamespace.DataType.INT16, TensorNamespace.DataType.INT8,\n        TensorNamespace.DataType.UINT8, TensorNamespace.DataType.UINT16,\n        TensorNamespace.DataType.BOOL, TensorNamespace.DataType.UTF8,\n        TensorNamespace.DataType.FLOAT, TensorNamespace.DataType.DOUBLE,\n        TensorNamespace.DataType.UNKNOWN)","tryCatchPattern":"try {\n    val arr = ndarrayFromNameSpaceTensor(tensor)\n} catch (e: IllegalArgumentException) {\n    if (e.message?.startsWith(\"Illegal type found for conversion\") == true)\n        return decodeManually(tensor) // custom raw decode\n    throw e\n}","preventionTips":["Keep the import library on the same version as the model exporter","Log inputTensor.dataType ordinal when imports fail","Convert exotic dtypes upstream to supported ones (float32/int32)","Pin nd4j versions in your build so protobuf enums cannot drift"],"tags":["kotlin","nd4j","dtype","enum"],"backgroundTag":"unsupported-dtype","analyzedSha":"4c22ac5fe4a8350d05d224e7f4499429f7f69c93","analyzedAt":"2026-09-07T05:27:04.714Z","contentChangedAt":"2026-09-07T05:27:04.714Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}