{"record":{"id":"6c5fe761ece7b56b","repo":"huggingface/candle","slug":"multiple-value-types-in-the-same-array-value-type","errorCode":null,"errorMessage":"multiple value-types in the same array {value_type:?}","messagePattern":"multiple value-types in the same array (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/quantized/gguf_file.rs","lineNumber":400,"sourceCode":"            &Self::U32(v) => w.write_u32::<LittleEndian>(v)?,\n            &Self::I32(v) => w.write_i32::<LittleEndian>(v)?,\n            &Self::U64(v) => w.write_u64::<LittleEndian>(v)?,\n            &Self::I64(v) => w.write_i64::<LittleEndian>(v)?,\n            &Self::F32(v) => w.write_f32::<LittleEndian>(v)?,\n            &Self::F64(v) => w.write_f64::<LittleEndian>(v)?,\n            &Self::Bool(v) => w.write_u8(u8::from(v))?,\n            Self::String(v) => write_string(w, v.as_str())?,\n            Self::Array(v) => {\n                // The `Value` type does not enforce that all the values in an Array have the same\n                // type.\n                let value_type = if v.is_empty() {\n                    // Doesn't matter, the array is empty.\n                    ValueType::U32\n                } else {\n                    let value_type: std::collections::HashSet<_> =\n                        v.iter().map(|elem| elem.value_type()).collect();\n                    if value_type.len() != 1 {\n                        crate::bail!(\"multiple value-types in the same array {value_type:?}\")\n                    }\n                    value_type.into_iter().next().context(\"empty value_type\")?\n                };\n                w.write_u32::<LittleEndian>(value_type.to_u32())?;\n                w.write_u64::<LittleEndian>(v.len() as u64)?;\n                for elem in v.iter() {\n                    elem.write(w)?\n                }\n            }\n        }\n        Ok(())\n    }\n}\n\nimpl ValueType {\n    fn from_u32(v: u32) -> Result<Self> {\n        let v = match v {\n            0 => Self::U8,","sourceCodeStart":382,"sourceCodeEnd":418,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/quantized/gguf_file.rs#L382-L418","documentation":"When writing GGUF metadata, Value::write derives one ValueType for a whole array from its elements and throws this if the elements have more than one distinct value type. GGUF arrays are homogeneous by spec: a single element-type tag precedes all values. Mixed arrays (e.g. a Vec<Value> containing both String and U32) are rejected.","triggerScenarios":"Calling Content::write (or the metadata write path) with a metadata map whose Value::Array contains heterogeneous elements, e.g. vec![Value::String(\"a\".into()), Value::U32(1)].","commonSituations":"Building metadata dynamically with a generic Vec<Value> that accidentally mixes scalars and strings; trying to encode a JSON-like object where fields have different types; a schema change in your model config causing mixed entries.","solutions":["Normalize array elements to one type before writing (e.g. stringify all entries, or widen numbers to u32).","Split heterogeneous data into separate single-typed metadata keys.","Use a JSON string metadata value (single String) if you need mixed-type structured data.","Add a pre-write assert that every element maps to the same value_type()."],"exampleFix":"// before\nlet vals = vec![Value::String(\"a\".into()), Value::U32(1)];\nmetadata.insert(\"my.mixed\".to_string(), Value::Array(vals));\n// after\nlet vals = vec![Value::String(\"a\".into()), Value::String(\"1\".into())];\nmetadata.insert(\"my.mixed\".to_string(), Value::Array(vals));","handlingStrategy":"validation","validationCode":"fn all_same_type(vals: &[gguf_file::Value]) -> bool {\n    use std::collections::HashSet;\n    vals.iter().map(|v| v.value_type()).collect::<HashSet<_>>().len() <= 1\n}\nif let Value::Array(vs) = &value, !all_same_type(vs) { bail!(\"mixed-type array\"); }","typeGuard":null,"tryCatchPattern":"match write_metadata(&mut w, &metadata) {\n    Ok(()) => (),\n    Err(e) if e.to_string().contains(\"multiple value-types\") => {\n        eprintln!(\"array is heterogeneous: {e}\");\n        return Err(Error::Msg(\"gguf arrays must be homogeneous\".into()));\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Normalize elements to a single type (e.g. all String) before building Value::Array","Encode mixed structured data as one JSON string metadata value instead of an array","Split heterogeneous fields into separate single-typed metadata keys","Add a debug assert checking value_type() uniformity when constructing arrays"],"tags":["candle","gguf","serialization","type-mismatch"],"backgroundTag":"unexpected-value-type","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}