{"record":{"id":"db1d6653f77fe255","repo":"pola-rs/polars","slug":"mutableutf8valuesarray-can-only-be-initialized-wit","errorCode":null,"errorMessage":"MutableUtf8ValuesArray can only be initialized with DataType::Utf8 or DataType::LargeUtf8","messagePattern":"MutableUtf8ValuesArray can only be initialized with DataType::Utf8 or DataType::LargeUtf8","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-arrow/src/array/utf8/mutable_values.rs","lineNumber":113,"sourceCode":"    /// * `offsets.last()` is greater than `values.len()`\n    /// * The `dtype`'s [`crate::datatypes::PhysicalType`] is equal to either `Utf8` or `LargeUtf8`.\n    ///\n    /// # Safety\n    /// This function is safe iff:\n    /// * the offsets are monotonically increasing\n    /// * The `values` between two consecutive `offsets` are not valid utf8\n    /// # Implementation\n    /// This function is `O(1)`\n    pub unsafe fn new_unchecked(\n        dtype: ArrowDataType,\n        offsets: Offsets<O>,\n        values: Vec<u8>,\n    ) -> Self {\n        try_check_offsets_bounds(&offsets, values.len())\n            .expect(\"The length of the values must be equal to the last offset value\");\n\n        if dtype.to_physical_type() != Self::default_dtype().to_physical_type() {\n            panic!(\n                \"MutableUtf8ValuesArray can only be initialized with DataType::Utf8 or DataType::LargeUtf8\"\n            )\n        }\n\n        Self {\n            dtype,\n            offsets,\n            values,\n        }\n    }\n\n    /// Returns the default [`ArrowDataType`] of this container: [`ArrowDataType::Utf8`] or [`ArrowDataType::LargeUtf8`]\n    /// depending on the generic [`Offset`].\n    pub fn default_dtype() -> ArrowDataType {\n        Utf8Array::<O>::default_dtype()\n    }\n\n    /// Initializes a new [`MutableUtf8ValuesArray`] with a pre-allocated capacity of items.","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-arrow/src/array/utf8/mutable_values.rs#L95-L131","documentation":"`MutableUtf8ValuesArray` only supports `DataType::Utf8` and `DataType::LargeUtf8` physical types; its dtype parameter is validated right after the offset bounds check. Passing any other dtype (Binary, List, etc.) would make subsequent typed value access unsound, so the constructor panics.","triggerScenarios":"Calling `MutableUtf8ValuesArray::new_unchecked(dtype, offsets, values)` with a dtype whose physical type differs from Utf8/LargeUtf8 — e.g. `Binary`, `FixedSizeBinary`, or a custom dtype.","commonSituations":"Generic mutable-array builders parameterized by dtype that route binary data into the UTF8 builder by mistake; dtype read from external metadata (schema files, IPC) mapping to the wrong variant; copy-paste of Utf8 code for a Binary column.","solutions":["Pass `DataType::Utf8` or `DataType::LargeUtf8` (match `O`: u32→Utf8, u64→LargeUtf8).","For binary data use `MutableBinaryValuesArray` instead.","Dispatch on the dtype before constructing so only UTF8 dtypes reach this builder.","If dtype comes from metadata, validate it upfront and return an error for non-UTF8 variants."],"exampleFix":"// before\nlet arr = unsafe { MutableUtf8ValuesArray::new_unchecked(dtype.clone(), offsets, values) };\n// after\nlet arr = match dtype {\n    ArrowDataType::Utf8 | ArrowDataType::LargeUtf8 =>\n        unsafe { MutableUtf8ValuesArray::new_unchecked(dtype.clone(), offsets, values) },\n    ArrowDataType::Binary | ArrowDataType::LargeBinary =>\n        MutableBinaryValuesArray::new_unchecked(dtype.clone(), offsets, values).into_boxed(),\n    _ => return Err(PolarsError::InvalidOperation(\"unsupported dtype\".into())),\n};","handlingStrategy":"type-guard","validationCode":"fn is_utf8_dtype(dtype: &ArrowDataType) -> bool {\n    matches!(dtype.to_storage(), ArrowDataType::Utf8 | ArrowDataType::LargeUtf8)\n}","typeGuard":"fn utf8_dtype_for<O: NativeOffset>() -> ArrowDataType {\n    if size_of::<O>() == 4 { ArrowDataType::Utf8 } else { ArrowDataType::LargeUtf8 }\n}","tryCatchPattern":null,"preventionTips":["Derive the dtype from the offset type (u32→Utf8, u64→LargeUtf8) instead of taking it as a parameter","Use `MutableBinaryValuesArray` for binary data, never the UTF8 builder","Validate externally-sourced dtypes before constructing typed builders"],"tags":["rust","arrow","utf8","dtype","panic"],"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"}