{"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/9b5d73fd00236295624374b075d16b1fe6ec6df9/crates/polars-arrow/src/array/utf8/mutable_values.rs#L95-L131","documentation":"new_unchecked compares dtype.to_physical_type() against the array's default dtype (Utf8 for i32 offsets, LargeUtf8 for i64 offsets). Anything else — Utf8View, Binary, Dictionary, or an extension whose storage is not Utf8/LargeUtf8 — panics with this message.","triggerScenarios":"Passing ArrowDataType::Utf8View (or Binary/BinaryView) offsets/values into the Utf8 values array; passing a Dictionary(Utf8) dtype; mixing a LargeUtf8 dtype with the i32-offset generic instantiation.","commonSituations":"Migrating string data to the view layout and reusing the old constructor; generic code parameterized over dtype that lands in the wrong constructor arm.","solutions":["Use ArrowDataType::Utf8 with O = i32, or LargeUtf8 with O = i64 — the physical type must match the offset width","For view-encoded strings use the view arrays (Utf8ViewArray / MutableBinViewArray<str>) instead","For extension types, construct with the underlying Utf8/LargeUtf8 storage dtype and re-tag afterwards"],"exampleFix":"// before\nlet arr = unsafe { MutableUtf8ValuesArray::<i32>::new_unchecked(ArrowDataType::Utf8View, offsets, values) };\n\n// after\nlet arr = unsafe { MutableUtf8ValuesArray::<i32>::new_unchecked(ArrowDataType::Utf8, offsets, values) };","handlingStrategy":"validation","validationCode":"fn utf8_dtype_ok<O: polars_arrow::offset::Offset>(dtype: &ArrowDataType) -> bool {\n    dtype.to_physical_type()\n        == MutableUtf8ValuesArray::<O>::default_dtype().to_physical_type()\n}","typeGuard":"fn is_utf8_physical(dtype: &ArrowDataType) -> bool {\n    matches!(dtype.to_physical_type(), polars_arrow::datatypes::PhysicalType::Utf8 | polars_arrow::datatypes::PhysicalType::LargeUtf8)\n}","tryCatchPattern":null,"preventionTips":["Match the dtype to the offset width: Utf8 with i32, LargeUtf8 with i64","Use the view arrays for Utf8View data instead of this constructor","Construct with the physical storage dtype and re-tag extensions afterwards"],"tags":["rust","polars","arrow","utf8","dtype"],"backgroundTag":"dtype-mismatch","analyzedSha":"9b5d73fd00236295624374b075d16b1fe6ec6df9","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}