pola-rs/polars · error

The length of the values must be equal to the last offset…

Error message

The length of the values must be equal to the last offset value

What it means

`MutableUtf8ValuesArray::new_unchecked` is an `unsafe` constructor that trusts the caller-supplied offsets and values, but it still validates that the last offset equals `values.len()` (via `try_check_offsets_bounds`) so the offsets actually index the value buffer. If the final offset doesn't match the values length, the resulting array would read out of bounds, so it `expect`-panics.

Solutions

  1. Ensure `offsets.last() == values.len()` before calling, or recompute offsets from the values with `Offsets::from_lengths` / `try_from_iter`.
  2. Use the safe `MutableUtf8ValuesArray::try_new` / `new_from_iter` constructors which validate and return `PolarsResult` instead of panicking.
  3. Fix the producer of `values` so the two buffers are built in lockstep.
  4. Run under debug/tests with the bounds check kept (`try_check_offsets_bounds` first) until correctness is proven.

Example fix

// before
let arr = unsafe { MutableUtf8ValuesArray::new_unchecked(dtype, offsets, values) };
// after
let offsets = Offsets::try_from(&values_lens)?; // or rebuild:
let arr = MutableUtf8ValuesArray::try_new(dtype, offsets, values)?;
Defensive patterns

Strategy: validation

Validate before calling

fn offsets_match(offsets: &Offsets<O>, values_len: usize) -> bool {
    *offsets.last() as usize == values_len
}

Prevention

When it happens

Trigger: Calling `MutableUtf8ValuesArray::new_unchecked(dtype, offsets, values)` where `*offsets.last()` != `values.len()` — e.g. offsets built for a shorter/longer value buffer, or offsets mutated after construction of `values`.

Common situations: Hand-rolled builders of UTF8 arrays in FFI/import code where bytes and offsets are produced by separate passes; truncating or extending the value buffer without recomputing offsets; porting code from `try_new` (which returns Err) to `new_unchecked` without moving the validation.

Related errors


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

Appendix: source

Thrown at crates/polars-arrow/src/array/utf8/mutable_values.rs:110

    ///
    /// # Panic
    /// This function does not panic iff:
    /// * `offsets.last()` is greater than `values.len()`
    /// * The `dtype`'s [`crate::datatypes::PhysicalType`] is equal to either `Utf8` or `LargeUtf8`.
    ///
    /// # Safety
    /// This function is safe iff:
    /// * the offsets are monotonically increasing
    /// * The `values` between two consecutive `offsets` are not valid utf8
    /// # Implementation
    /// This function is `O(1)`
    pub unsafe fn new_unchecked(
        dtype: ArrowDataType,
        offsets: Offsets<O>,
        values: Vec<u8>,
    ) -> Self {
        try_check_offsets_bounds(&offsets, values.len())
            .expect("The length of the values must be equal to the last offset value");

        if dtype.to_physical_type() != Self::default_dtype().to_physical_type() {
            panic!(
                "MutableUtf8ValuesArray can only be initialized with DataType::Utf8 or DataType::LargeUtf8"
            )
        }

        Self {
            dtype,
            offsets,
            values,
        }
    }

    /// Returns the default [`ArrowDataType`] of this container: [`ArrowDataType::Utf8`] or [`ArrowDataType::LargeUtf8`]
    /// depending on the generic [`Offset`].
    pub fn default_dtype() -> ArrowDataType {
        Utf8Array::<O>::default_dtype()

View on GitHub (pinned to fe841f959e)