{"record":{"id":"050f838015c4a326","repo":"huggingface/candle","slug":"unexpected-dtype-for-got-expected-dt","errorCode":null,"errorMessage":"unexpected dtype for {}, got {:?}, expected {dt:?}","messagePattern":"unexpected dtype for (.+?), got (.+?), expected (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-onnx/src/eval.rs","lineNumber":315,"sourceCode":"                    match &d.value {\n                        Some(onnx::tensor_shape_proto::dimension::Value::DimValue(v)) => {\n                            if *v as usize != dim {\n                                bail!(\n                                    \"unexpected dim {idx} for {}, got {:?}, expected {:?}\",\n                                    input.name,\n                                    shape.dim,\n                                    tensor.shape()\n                                )\n                            }\n                        }\n                        // We do not check equality constraints for the DimParam dimensions for now.\n                        Some(onnx::tensor_shape_proto::dimension::Value::DimParam(_)) | None => (),\n                    }\n                }\n            }\n        };\n        if dt != tensor.dtype() {\n            bail!(\n                \"unexpected dtype for {}, got {:?}, expected {dt:?}\",\n                input.name,\n                tensor.dtype()\n            )\n        }\n    }\n    // The nodes are topologically sorted so we can just process them in order.\n    for node in graph.node.iter() {\n        let get = |input_name: &str| match values.get(input_name) {\n            Some(value) => Ok(value),\n            None => bail!(\"cannot find {input_name} for op '{}'\", node.name),\n        };\n        let get_opt = |i: usize| {\n            node.input\n                .get(i)\n                .filter(|s: &&String| !s.is_empty())\n                .map(|s| get(s))\n        };","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-onnx/src/eval.rs#L297-L333","documentation":"The final input check compares the inferred candle DType (from the declared elem_type) with the actual dtype of the supplied tensor. If they differ, this error is thrown, printing the supplied tensor's dtype and the expected one. ONNX requires inputs to match the declared element type exactly.","triggerScenarios":"simple_eval with, e.g., an f32 tensor where the model declares int64 input (common with token ids), or an i64 tensor where the model declares f32, or f16 model inputs fed with f32 data.","commonSituations":"Token ids passed as f32/u32 instead of i64; images normalized into f32 arrays fed to a model exported with f16 inputs; numpy default float64 vs model float32.","solutions":["Convert the input tensor to the expected dtype printed in the error (e.g. to_dtype(DType::I64) in candle, .to(torch.int64) in PyTorch before export-time contract).","Align tokenization/embedding code so ids are produced as i64.","Fix preprocessing so image tensors match the exported dtype (f16 vs f32).","Add a pre-eval loop asserting tensor.dtype() against model input metadata for all inputs."],"exampleFix":"// before\nlet ids = Tensor::from_vec(ids_u32, shape)?.to_dtype(DType::F32)?;\n// after\nlet ids = Tensor::from_vec(ids_u32, shape)?.to_dtype(DType::I64)?; // model expects int64","handlingStrategy":"validation","validationCode":"fn dtypes_match(model: &onnx::ModelProto, inputs: &HashMap<String, Value>) -> Vec<String> {\n    let mut bad = vec![];\n    if let Some(g) = &model.graph {\n        for i in &g.input {\n            if let (Some(t), Some(onnx::type_proto::Value::TensorType(tt))) =\n                (inputs.get(&i.name), &i.r#type.value)\n            {\n                if let Some(dt) = candle_onnx::DataType::try_from(tt.elem_type).ok()\n                    .and_then(candle_onnx::eval::dtype)\n                {\n                    if dt != t.dtype() { bad.push(i.name.clone()); }\n                }\n            }\n        }\n    }\n    bad\n}","typeGuard":null,"tryCatchPattern":"match simple_eval(&model, inputs) {\n    Err(e) if e.to_string().starts_with(\"unexpected dtype for\") => {\n        anyhow::bail!(\"convert input to the expected dtype shown in the error (e.g. to_dtype(DType::I64))\")\n    }\n    r => r?,\n}","preventionTips":["Cast token ids to i64 and embeddings to f32 as per the export contract","Match f16/f32 between export and inference inputs","Watch numpy float64 vs model float32","Assert tensor.dtype() against graph.input metadata in tests"],"tags":["onnx","dtype-mismatch","eval","input-validation"],"backgroundTag":"dtype-mismatch","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}