huggingface/candle · error

multiple value-types in the same array {value_type:?}

Error message

multiple value-types in the same array {value_type:?}

What it means

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.

Source

Thrown at candle-core/src/quantized/gguf_file.rs:400

            &Self::U32(v) => w.write_u32::<LittleEndian>(v)?,
            &Self::I32(v) => w.write_i32::<LittleEndian>(v)?,
            &Self::U64(v) => w.write_u64::<LittleEndian>(v)?,
            &Self::I64(v) => w.write_i64::<LittleEndian>(v)?,
            &Self::F32(v) => w.write_f32::<LittleEndian>(v)?,
            &Self::F64(v) => w.write_f64::<LittleEndian>(v)?,
            &Self::Bool(v) => w.write_u8(u8::from(v))?,
            Self::String(v) => write_string(w, v.as_str())?,
            Self::Array(v) => {
                // The `Value` type does not enforce that all the values in an Array have the same
                // type.
                let value_type = if v.is_empty() {
                    // Doesn't matter, the array is empty.
                    ValueType::U32
                } else {
                    let value_type: std::collections::HashSet<_> =
                        v.iter().map(|elem| elem.value_type()).collect();
                    if value_type.len() != 1 {
                        crate::bail!("multiple value-types in the same array {value_type:?}")
                    }
                    value_type.into_iter().next().context("empty value_type")?
                };
                w.write_u32::<LittleEndian>(value_type.to_u32())?;
                w.write_u64::<LittleEndian>(v.len() as u64)?;
                for elem in v.iter() {
                    elem.write(w)?
                }
            }
        }
        Ok(())
    }
}

impl ValueType {
    fn from_u32(v: u32) -> Result<Self> {
        let v = match v {
            0 => Self::U8,

View on GitHub (pinned to d5fee525bf)

Solutions

  1. Normalize array elements to one type before writing (e.g. stringify all entries, or widen numbers to u32).
  2. Split heterogeneous data into separate single-typed metadata keys.
  3. Use a JSON string metadata value (single String) if you need mixed-type structured data.
  4. Add a pre-write assert that every element maps to the same value_type().

Example fix

// before
let vals = vec![Value::String("a".into()), Value::U32(1)];
metadata.insert("my.mixed".to_string(), Value::Array(vals));
// after
let vals = vec![Value::String("a".into()), Value::String("1".into())];
metadata.insert("my.mixed".to_string(), Value::Array(vals));
Defensive patterns

Strategy: validation

Validate before calling

fn all_same_type(vals: &[gguf_file::Value]) -> bool {
    use std::collections::HashSet;
    vals.iter().map(|v| v.value_type()).collect::<HashSet<_>>().len() <= 1
}
if let Value::Array(vs) = &value, !all_same_type(vs) { bail!("mixed-type array"); }

Try / catch

match write_metadata(&mut w, &metadata) {
    Ok(()) => (),
    Err(e) if e.to_string().contains("multiple value-types") => {
        eprintln!("array is heterogeneous: {e}");
        return Err(Error::Msg("gguf arrays must be homogeneous".into()));
    }
    Err(e) => return Err(e.into()),
}

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of huggingface/candle@d5fee525bf (2026-09-02). Data as JSON: /api/errors/6c5fe761ece7b56b. Report an issue: GitHub.