{"record":{"id":"4f0fd4f446fe47b8","repo":"pola-rs/polars","slug":"read-an-array-from-a-non-array-data-type","errorCode":null,"errorMessage":"read an Array from a non-Array data type","messagePattern":"read an Array from a non-Array data type","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-json/src/json/deserialize.rs","lineNumber":551,"sourceCode":"            allow_extra_fields_in_struct,\n        )?)),\n        adt => polars_bail!(\n            ComputeError: \"deserialization from JSON is not implemented for `{adt:?}`\"\n        ),\n    }\n}\n\npub fn deserialize(\n    json: &BorrowedValue,\n    dtype: ArrowDataType,\n    allow_extra_fields_in_struct: bool,\n) -> PolarsResult<Box<dyn Array>> {\n    match json {\n        BorrowedValue::Array(rows) => match dtype {\n            ArrowDataType::LargeList(inner) => {\n                _deserialize(rows, inner.dtype, allow_extra_fields_in_struct)\n            },\n            _ => todo!(\"read an Array from a non-Array data type\"),\n        },\n        _ => _deserialize(&[json], dtype, allow_extra_fields_in_struct),\n    }\n}\n\nfn check_err_idx<'a>(\n    rows: &[impl Borrow<BorrowedValue<'a>>],\n    err_idx: usize,\n    type_name: &'static str,\n) -> PolarsResult<()> {\n    if err_idx != rows.len() {\n        polars_bail!(\n            ComputeError:\n            r#\"error deserializing value \"{:?}\" as {}.\n\nTry increasing `infer_schema_length` or specifying a schema.\"#,\n            rows[err_idx].borrow(), type_name,\n        )","sourceCodeStart":533,"sourceCodeEnd":569,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-json/src/json/deserialize.rs#L533-L569","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass ArrowDataType::LargeList with the correct inner dtype for array-shaped JSON payloads.","If the payload is a single object, don't wrap it in an array or use the single-value path (_deserialize(&[json], ...)).","Align the declared schema with the actual JSON structure before deserializing.","Check the upstream data source for schema changes (list vs struct)."],"exampleFix":"// before\nlet dtype = ArrowDataType::Struct(fields);\ndeserialize(&json_array, dtype, false)?; // json is an Array\n// after\nlet dtype = ArrowDataType::LargeList(Arc::new(Field::new(\"item\", ArrowDataType::Int64, true)));\ndeserialize(&json_array, dtype, false)?;","handlingStrategy":"type-guard","validationCode":"if let BorrowedValue::Array(_) = &json {\n    assert!(matches!(dtype, ArrowDataType::LargeList(_)), \"Array JSON requires LargeList dtype\");\n}","typeGuard":"fn json_matches_dtype(json: &BorrowedValue, dtype: &ArrowDataType) -> bool {\n    match json {\n        BorrowedValue::Array(_) => matches!(dtype, ArrowDataType::LargeList(_)),\n        _ => true,\n    }\n}","tryCatchPattern":"// deserialize panics via todo! on mismatch; narrow before calling\nassert!(json_matches_dtype(&json, &dtype), \"payload/dtype mismatch: array JSON needs LargeList\");","preventionTips":["Keep declared schema in sync with the actual JSON payload shape","Wrap single objects in an array only when using LargeList dtype, and vice versa","Add schema-drift tests for upstream producers that may switch list to struct"],"tags":["json","arrow","schema","dtype"],"backgroundTag":"dtype-mismatch","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}