pola-rs/polars · error

read an Array from a non-Array data type

Error message

read an Array from a non-Array data type

What it means

deserialize expects the top-level JSON value to be an Array when the dtype is LargeList; if the JSON value is an Array but the declared dtype is anything other than LargeList, it hits a todo! panic. In other words an Array/struct dtype mismatch between the JSON payload and the provided Arrow schema.

Solutions

  1. Pass ArrowDataType::LargeList with the correct inner dtype for array-shaped JSON payloads.
  2. If the payload is a single object, don't wrap it in an array or use the single-value path (_deserialize(&[json], ...)).
  3. Align the declared schema with the actual JSON structure before deserializing.
  4. Check the upstream data source for schema changes (list vs struct).

Example fix

// before
let dtype = ArrowDataType::Struct(fields);
deserialize(&json_array, dtype, false)?; // json is an Array
// after
let dtype = ArrowDataType::LargeList(Arc::new(Field::new("item", ArrowDataType::Int64, true)));
deserialize(&json_array, dtype, false)?;
Defensive patterns

Strategy: type-guard

Validate before calling

if let BorrowedValue::Array(_) = &json {
    assert!(matches!(dtype, ArrowDataType::LargeList(_)), "Array JSON requires LargeList dtype");
}

Type guard

fn json_matches_dtype(json: &BorrowedValue, dtype: &ArrowDataType) -> bool {
    match json {
        BorrowedValue::Array(_) => matches!(dtype, ArrowDataType::LargeList(_)),
        _ => true,
    }
}

Try / catch

// deserialize panics via todo! on mismatch; narrow before calling
assert!(json_matches_dtype(&json, &dtype), "payload/dtype mismatch: array JSON needs LargeList");

Prevention

When it happens

Trigger: Calling deserialize (public) with json = BorrowedValue::Array and dtype something other than ArrowDataType::LargeList — e.g. passing Struct, List, or FixedSizeList dtype for a JSON array payload.

Common situations: Manually building a decoder with the wrong dtype for the JSON payload; schema drift where a column changed from list-of-X to struct; feeding JSON arrays into a non-list column decoder.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/4f0fd4f446fe47b8. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-json/src/json/deserialize.rs:551

            allow_extra_fields_in_struct,
        )?)),
        adt => polars_bail!(
            ComputeError: "deserialization from JSON is not implemented for `{adt:?}`"
        ),
    }
}

pub fn deserialize(
    json: &BorrowedValue,
    dtype: ArrowDataType,
    allow_extra_fields_in_struct: bool,
) -> PolarsResult<Box<dyn Array>> {
    match json {
        BorrowedValue::Array(rows) => match dtype {
            ArrowDataType::LargeList(inner) => {
                _deserialize(rows, inner.dtype, allow_extra_fields_in_struct)
            },
            _ => todo!("read an Array from a non-Array data type"),
        },
        _ => _deserialize(&[json], dtype, allow_extra_fields_in_struct),
    }
}

fn check_err_idx<'a>(
    rows: &[impl Borrow<BorrowedValue<'a>>],
    err_idx: usize,
    type_name: &'static str,
) -> PolarsResult<()> {
    if err_idx != rows.len() {
        polars_bail!(
            ComputeError:
            r#"error deserializing value "{:?}" as {}.

Try increasing `infer_schema_length` or specifying a schema."#,
            rows[err_idx].borrow(), type_name,
        )

View on GitHub (pinned to fe841f959e)