{"record":{"id":"94c15b1aad8fbfb5","repo":"pola-rs/polars","slug":"map-anyvalue-payload-must-be-a-struct","errorCode":null,"errorMessage":"Map AnyValue payload must be a Struct","messagePattern":"Map AnyValue payload must be a Struct","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/datatypes/any_value.rs","lineNumber":260,"sourceCode":"            #[cfg(feature = \"dtype-datetime\")]\n            Datetime(_, tu, tz) => DataType::Datetime(*tu, (*tz).cloned()),\n            #[cfg(feature = \"dtype-datetime\")]\n            DatetimeOwned(_, tu, tz) => {\n                DataType::Datetime(*tu, tz.as_ref().map(|v| v.as_ref().clone()))\n            },\n            #[cfg(feature = \"dtype-duration\")]\n            Duration(_, tu) => DataType::Duration(*tu),\n            #[cfg(feature = \"dtype-categorical\")]\n            Categorical(_, _) | CategoricalOwned(_, _) => {\n                unimplemented!(\"can not get dtype of Categorical AnyValue\")\n            },\n            #[cfg(feature = \"dtype-categorical\")]\n            Enum(_, _) | EnumOwned(_, _) => unimplemented!(\"can not get dtype of Enum AnyValue\"),\n            List(s) => DataType::List(Box::new(s.dtype().clone())),\n            #[cfg(feature = \"dtype-map\")]\n            Map(s) => {\n                let DataType::Struct(fields) = s.dtype() else {\n                    unimplemented!(\"Map AnyValue payload must be a Struct\")\n                };\n                DataType::Map(\n                    Box::new(fields[0].dtype().clone()),\n                    Box::new(fields[1].dtype().clone()),\n                )\n            },\n            #[cfg(feature = \"dtype-array\")]\n            Array(s, size) => DataType::Array(Box::new(s.dtype().clone()), *size),\n            #[cfg(feature = \"dtype-struct\")]\n            Struct(_, _, fields) => DataType::Struct(fields.to_vec()),\n            #[cfg(feature = \"dtype-struct\")]\n            StructOwned(payload) => DataType::Struct(payload.1.clone()),\n            #[cfg(feature = \"dtype-decimal\")]\n            Decimal(_, p, s) => DataType::Decimal(*p, *s),\n            #[cfg(feature = \"object\")]\n            Object(o) => DataType::Object(o.type_name()),\n            #[cfg(feature = \"object\")]\n            ObjectOwned(o) => DataType::Object(o.0.type_name()),","sourceCodeStart":242,"sourceCodeEnd":278,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-core/src/datatypes/any_value.rs#L242-L278","documentation":"A Map AnyValue holds a StructArray whose first field must be the keys column and second the values column. dtype() derives DataType::Map(key, value) from those fields and asserts the payload is a Struct via a let-else; any other payload type is an internal invariant violation and panics.","triggerScenarios":"Calling .dtype() on AnyValue::Map whose inner array is not a StructArray — i.e. a corrupted or wrongly constructed Map value, e.g. built manually with an incorrect array type or produced by an unsafe/FFI path that mislabels the payload.","commonSituations":"Reading Map columns produced through arrow FFI or custom IO code where the inner array type was not preserved; hand-constructing AnyValue::Map with a non-struct array in bindings/extensions.","solutions":["Construct Map AnyValues only with a StructArray of exactly [key_field, value_field]; fix the constructor call.","Re-read the column through polars' own readers (read_ipc/parquet/json) instead of hand-built arrays.","Check s.dtype() on the source Series and verify it is DataType::Map before extracting AnyValues.","If hitting this from FFI, validate the incoming Arrow array layout matches the Map spec (entries struct with 2 fields)."],"exampleFix":"// before\nlet av = AnyValue::Map(Arc::new(ListArray::from(...))); // wrong payload\n// after\nlet entries = StructArray::new(\n    fields![keys_field, values_field], [keys_arr, values_arr], None,\n);\nlet av = AnyValue::Map(Arc::new(entries));","handlingStrategy":"validation","validationCode":"// before building AnyValue::Map\nassert_eq!(entries.data_type(), &ArrowDataType::Struct(...));\nassert_eq!(entries.fields().len(), 2); // keys, values","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only construct Map AnyValues from a 2-field StructArray (keys, values)","Read Map columns with polars' native readers rather than hand-built arrays","Validate Arrow layouts when bridging via FFI"],"tags":["polars","panic","map","invariant"],"backgroundTag":"internal-invariant-violation","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"}