deeplearning4j/deeplearning4j · error · java.lang.IllegalArgumentException

Illegal type found for conversion

Error message

Illegal type found for conversion ${dtype}

What it means

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.

Solutions

  1. Upgrade nd4j/samediff-import to a version whose convertNd4jDataTypeFromNameSpaceTensorDataType covers the exporter's DataType enum
  2. Log the offending inputTensor.dataType ordinal and inspect the producing model
  3. Re-export the model with a supported data type (e.g. cast unsupported dtypes to float32/int32 upstream)
  4. Wrap the conversion in try/catch on IllegalArgumentException and fall back to a manual tensor decode

Example fix

// before: new enum value from newer exporter hits unhandled path
val t = tensor { dataType = NEW_DTYPE_ORDINAL }
// after: normalize dtype before import
val t = tensor { dataType = FLOAT.ordinal } // or upgrade the import library
Defensive patterns

Strategy: try-catch

Validate before calling

val dt = TensorNamespace.DataType.forNumber(inputTensor.dataType)
if (dt == null || dt == TensorNamespace.DataType.UNRECOGNIZED) {
    throw ModelImportException("Unknown tensor data type ordinal ${inputTensor.dataType}")
}

Type guard

fun TensorProto.hasConvertibleType(): Boolean =
    TensorNamespace.DataType.forNumber(dataType) in setOf(
        TensorNamespace.DataType.INT64, TensorNamespace.DataType.INT32,
        TensorNamespace.DataType.INT16, TensorNamespace.DataType.INT8,
        TensorNamespace.DataType.UINT8, TensorNamespace.DataType.UINT16,
        TensorNamespace.DataType.BOOL, TensorNamespace.DataType.UTF8,
        TensorNamespace.DataType.FLOAT, TensorNamespace.DataType.DOUBLE,
        TensorNamespace.DataType.UNKNOWN)

Try / catch

try {
    val arr = ndarrayFromNameSpaceTensor(tensor)
} catch (e: IllegalArgumentException) {
    if (e.message?.startsWith("Illegal type found for conversion") == true)
        return decodeManually(tensor) // custom raw decode
    throw e
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07). Data as JSON: /api/errors/68e8ba04e63537df. Report an issue: GitHub.

Appendix: source

Thrown at nd4j/samediff-import/samediff-import-api/src/main/kotlin/org/nd4j/samediff/frameworkimport/IRProtobufExtensions.kt:426

                    return Nd4j.create(newStringList).reshape(*shape)
                }
                throw IllegalArgumentException("Shape of ${Arrays.toString(shape)} did not match length ${stringList.size}")
            }
            return Nd4j.create(stringList).reshape(*shape)
        }

        DataType.UNKNOWN -> {
            val ret = Nd4j.empty()
            return ret
        }

        else -> {
            return loadDataBufferFromRawData(inputTensor)
        }

    }

    throw IllegalArgumentException("Illegal type found for conversion ${dtype}")
}

fun loadDataBufferFromRawData(inputTensor: TensorNamespace.TensorProto): INDArray {
    val shape = inputTensor.dimsList.toLongArray()
    val dtype = convertNd4jDataTypeFromNameSpaceTensorDataType(TensorNamespace.DataType.values()[inputTensor.dataType])
    val byteArray = inputTensor.rawData.toByteArray()
    //note: scalar can be zero
    var totalLen = ArrayUtil.prod(*shape)


    if(dtype == DataType.UTF8) {
        val rawDataBuffer =  Nd4j.getDataBufferFactory().createUtf8Buffer(byteArray,byteArray.size.toLong())
        if(shape.isNotEmpty() && totalLen > 0) {
            if(rawDataBuffer.length() > 0) {
                val stringInput = java.lang.String(byteArray).toString()
                return Nd4j.create(stringInput)
            }
            return Nd4j.empty(dtype)

View on GitHub (pinned to 4c22ac5fe4)